3

I am trying to read the text of the elements in a dropdown menu using Selenium, but when I use "element.text" I get a stale element exception. This is my code:

 list_options = driver.find_elements_by_class_name("select2 results__option") for l in list_options: if l.text == "DOI": l.click() 

This is the error message I've been getting:

if l.text == "DOI": File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site- packages/selenium/webdriver/remote/webelement.py", line 76, in text return self._execute(Command.GET_ELEMENT_TEXT)['value'] File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute return self._parent.execute(command, params) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=75.0.3770.80) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.1 x86_64) 

Any help would be appreciated as I'm new to Selenium. Thanks!

5
  • It's not clear from your question, are you going to a new page by click()ing, then going back to the previous page and trying to go to the next item in a list of previously found elements? Commented Jun 6, 2019 at 21:31
  • The clicking is selecting an option from a dropdown menu. It isn't going to a new page or anything. Commented Jun 6, 2019 at 21:34
  • Can you please post your select node html, make sure to add atleast one item. Commented Jun 6, 2019 at 22:16
  • @Firestarter Even before you face StaleElementReferenceException this line of code driver.find_elements_by_class_name("select2 results__option") will raise Invalid selector: Compound class names not permitted in the first place Commented Jun 7, 2019 at 8:20
  • to make this reproducible, shouldn't you post also the page you're scraping? (and maybe cut out the irrelevant parts) as according to: stackoverflow.com/help/minimal-reproducible-example Commented Jun 7, 2019 at 12:45

1 Answer 1

0

When you are clicking on the item, the page is getting refreshed, and the element you have a reference to (stored in a variable) is stale. The page refreshes changes things in such a way that the exact element you stored is no longer on the page. To overcome this, you need to re-assign the drop-down list inside the for loop.

Try the below code.

list_options = driver.find_elements_by_css_selector('.select2.results__option') for l in range(len(list_options)): new_list_options = driver.find_elements_by_css_selector('.select2.results__option') if new_list_options[l].text == "DOI": new_list_options[l].click() 
Sign up to request clarification or add additional context in comments.

5 Comments

find_elements_by_class_name("select2 results__option") ???
@DebanjanB : I know it always good to use css selector instead of class name.However the problem is i guess the page is getting refreshed when OP clicked on drop down item.So that was the solution.I can modify this to css selector.
I should have been explicit earlier but find_elements_by_class_name("select2 results__option") will always raise a Invalid selector: Compound class names not permitted
Thank you! This worked but I had to change it to finding the elements by class name:
@Firestarter : Happy to help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.