0

Sometimes when searching for the secondbutton the site doesn't load correctly and therefore I want to check if the firstbutton is available so that way I know the site loaded correctly. Currently it only checks once if the firstbutton is available and if this is true it stops the loop. So how do I loop this code so that it checks everytime if the firstbutton is available even after running the while not code in the else function.

The code after the firstbutton and secondbutton cannot be the same so something like Prophet's answer wouldn't work.

while not (driver.find_elements(By.XPATH, "firstbutton")):
    driver.get('different url')
    driver.refresh()
else: 
    secondButton = driver.find_elements(By.XPATH, "secondbutton")
    while not (driver.find_elements(By.XPATH, "secondbutton")):
        time.sleep(1)
        driver.refresh()
secondbutton[0].click()
3
  • while not (driver.find_elements(By.XPATH, "secondbutton")) or not (driver.find_elements(By.XPATH, "firstbutton")) ? Commented Dec 30, 2021 at 16:28
  • those are functions from selenium webdriver Commented Dec 30, 2021 at 16:41
  • I meant replacing your second while condition with above. Replace while not (driver.find_elements(By.XPATH, "secondbutton")) with while not (driver.find_elements(By.XPATH, "secondbutton")) or not (driver.find_elements(By.XPATH, "firstbutton")) so unless it finds both first button and second button it will keep on refreshing in second while loop. Commented Dec 30, 2021 at 16:53

2 Answers 2

1
# wait while firstbutton is present
while not (driver.find_elements(By.XPATH, "firstbutton")):
    driver.get('different url')
    driver.refresh()

# firstbutton is found

secondButton = driver.find_elements(By.XPATH, "secondbutton")
# keep waiting and refreshing till we have both first and second button
while (not (driver.find_elements(By.XPATH, "secondbutton")) or
       not (driver.find_elements(By.XPATH, "firstbutton"))):         
    time.sleep(1)
    driver.refresh()
secondbutton[0].click()

should work in your case.

Sign up to request clarification or add additional context in comments.

3 Comments

This wouldn't work because after it looks for the firstbutton for the first time it won't do it again. And sometimes while looking for the secondbutton the site will crash and will only load after visiting a different site. So before looking at the second button, it must be checked whether the first button is there, because if the first button is not there, the url must be changed, while if the second button is not there, the page needs to refresh until the button is there.
So you mean to say that while firstbutton is found and we are waiting for secondbutton, the site might crash? and the subsequent code within second while block would raise an exception? - in this case it might be good idea to wrap the whole code in a try/except block and then add a few retries in case there is an exception so that whole code block since first while loop would get re-executed. Or you are looking for some selenium specific answer, in which caase I will remove the answer and will let others take a crack at it.
The try/except function is probably the best solution thanks. I set your answer as correct and will edit the comment to the answer. Thanks again and happy new years
0

Looks like you are trying to refresh the page until both first_button and the second_button are presented there?
If so all what you need is to refresh until both elements are found.
Something like this:

first_button = driver.find_elements(By.XPATH, "firstbutton")
second_button = driver.find_elements(By.XPATH, "secondbutton")
while not (first_button and second_button):
    first_button = driver.find_elements(By.XPATH, "firstbutton")
    second_button = driver.find_elements(By.XPATH, "secondbutton")
    time.sleep(1)
secondbutton[0].click()

3 Comments

Both buttons need to have different code after finding the buttons so this wouldn't work. I'm going to edit the question so this will become clear.
I'm not sure I understand the case. Can you explain yourself more clear?
When it doesn't find the firstbutton I want it to visit a different website and then refresh and when it finds the secondbutton to loop until it can click the secondbutton. Your solution makes it so that when both buttons are found the secondbutton will be pressed but it won't execute the code when it finds just the firstbutton. I will edit this thanks for letting me know again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.