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()
CodePudding user response:
# 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.
CodePudding user response:
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()