Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Waiting (explicitly and implicitly) not working in Python #4936

Closed
lukeis opened this issue Mar 4, 2016 · 4 comments
Closed

Waiting (explicitly and implicitly) not working in Python #4936

lukeis opened this issue Mar 4, 2016 · 4 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 4936

I'm trying to iterate through all the dropdown-list options on a page. The page reloads
whenever a new option is chosen, which means I need to find the select element again
for every option.

I'm using the Selenium Python Client Driver and ChromeDriver.

    browser = webdriver.Chrome()
    browser.get('http://www.bced.gov.bc.ca/apps/imcl/imclWeb/Home.do')
    city_list = browser.find_element_by_id('citySelect')
    city_options = city_list.find_elements_by_tag_name('option')
    for option in city_options:
        print option.text.strip()
        option.click()  

This will fail because of the page reload whenever a new option is selected.

In this code snippet, I try to find the element again after a new option is selected,
before continuing.

    browser = webdriver.Chrome()
    browser.get('http://www.bced.gov.bc.ca/apps/imcl/imclWeb/Home.do')
    city_list = Select(browser.find_element_by_id('citySelect'))
    num_cities = len(city_list.options)
    for i in xrange(1, num_cities):
        browser.implicitly_wait(10) # doesn't wait
        city_select_element = WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_id('citySelect'))
# also doesn't wait
        Select(city_select_element).select_by_index(i)

I keep getting a StaleElementReferenceException because I try to use the element before
it exists, and the driver isn't properly waiting for the element to appear. The waiting
appears as if it hasn't been implemented yet, as if there is simply a 'pass' statement
in their methods.

I'd prefer not to use time.sleep() or a while true loop that breaks when the element
is found.

Selenium version: 2.28.0
OS: Ubuntu 12.04
Browser: Chrome
Browser version: 23.0.1271.97

Reported by Dennis.I.89 on 2012-12-22 05:27:56

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Based on the waiting examples shown at http://seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits

Reported by Dennis.I.89 on 2012-12-22 05:29:59

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

This is expected behavior. If the page reloads, any found elements will go stale. In
this case an exact duplicate page will have all the same selectors so a find on a page
that is about to be refreshed will return the elements (without waiting), but then
as you experienced you'll get stale elements. The solution is to wait for the expected
condition of the element to go stale.

Here's how you can accomplish what you were doing in your sample:

from selenium import webdriver as w
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
d = w.Chrome()
d.get('http://www.bced.gov.bc.ca/apps/imcl/imclWeb/Home.do')
city_options = d.find_elements_by_css_selector('#citySelect option')
wait = WebDriverWait(d, 5)
# the first option is just 'city' which doesn't update the page when selected.
for x in range(1,len(city_options)):
    option = city_options[x]
    print option.text.strip()
    option.click()
    wait.until(EC.staleness_of(option))
    city_options = d.find_elements_by_css_selector('#citySelect option')


feel free to follow up with more questions like this on the selenium users mailing
list:
https://groups.google.com/forum/?fromgroups#!forum/selenium-users

Reported by luke.semerau on 2012-12-29 21:13:58

  • Status changed: WorkingAsIntended
  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Okay, thanks for the info. I found a workaround not long after I created this issue.

Perhaps this behaviour should be mentioned in a note on the docs? Just so others that
have the same issue don't think it's a bug.

Reported by Dennis.I.89 on 2012-12-30 04:34:41

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:16:30

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant