You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
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
The text was updated successfully, but these errors were encountered:
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
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.
Originally reported on Google Code with ID 4936
Reported by
Dennis.I.89
on 2012-12-22 05:27:56The text was updated successfully, but these errors were encountered: