I just can't figure this out. It seems I've tried everything, from WebDriverWait, try except statements, driver.implicitly_wait(60) and everything in between.
My code has the following structure:
while True:
element1 = driver.find_element_by_xpath(xpath1)
element2 = driver.find_element_by_xpath(xpath2)
wait = WebDriverWait(driver, 30)
element1 = wait.until(EC.visibility_of_element_located((By.XPATH, xpath1)))
element2 = wait.until(EC.visibility_of_element_located((By.XPATH, xpath2)))
if element1.text == 'some string': # get the exception here
# do something
elif element2.text == 'some string': # or here
# do something
driver.refresh()
I've tried implementing different expected conditions, none work. How can the element become stale between confirming it's visible to calling .text? Is it because the page is still loading? If so, how can I fix this?
SOLUTION: Ok, this seems to be working. If it fails at some point, I'll update this post.
while True:
try:
wait = WebDriverWait(driver, 30)
element1 = wait.until(EC.visibility_of_element_located((By.XPATH, xpath1)))
element2 = wait.until(EC.visibility_of_element_located((By.XPATH, xpath2)))
if element1.text == 'some string': # get the exception here
# do something
elif element2.text == 'some string': # or here
# do something
except (StaleElementReferenceException, TimeoutException):
continue
time.sleep(30)
driver.refresh()