Selenium TimeoutException (and NoSuchElementException) even after waiting explicitly. Weird fix: driver.page_source

31 views Asked by At

I've been trying all sorts of solutions to find an element not being found, even after waiting.

I ended up finding a fix by executing driver.page_source before trying to find the element, which seems to load the page and allow the element to be found, however I don't understand why this works.

Here is my original code:

driver.find_element('xpath','btn[text()="Next page"]').click()  # push next page button - loads new page 

time.sleep(10)
waiter = WebDriverWait(driver, 30)
waiter.until(EC.visibility_of_element_located((By.XPATH, '//label[text()="Time"]'))) #this returns Timeout Error

I also tried time.sleep(30) followed by driver.find_element('xpath','//label[text()="Time"]'), but to no avail (returns NoSuchElementException).

If I ran my code and had it wait even a minute to execute driver.find_element('xpath','//label[text()="Time"]'), this wouldn't work. However, if I waited the 30 seconds myself and then ran driver.find_element('xpath','//label[text()="Time"]') in the console manually, then it would work...

While trying to figure out what was happening, I ended up finding a solution:

driver.find_element('xpath','btn[text()="Next page"]').click() # push next page button - loads new page

driver.page_source  # seems to force the page to load?

waiter = WebDriverWait(driver, 10) 
waiter.until(EC.visibility_of_element_located((By.XPATH, '//label[text()="Time"]')))  # no more error

I'm hoping someone can provide some insight as to why:

  1. Manually inputting the code after the wait time works (sometimes)
  2. Executing driver.page_source in my code before the waiter makes it work fine
0

There are 0 answers