2

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!

supputuri
  • 13,644
  • 2
  • 21
  • 39
Firestarter
  • 86
  • 1
  • 11
  • 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? – G. Anderson Jun 06 '19 at 21:31
  • The clicking is selecting an option from a dropdown menu. It isn't going to a new page or anything. – Firestarter Jun 06 '19 at 21:34
  • Can you please post your select node html, make sure to add atleast one item. – supputuri Jun 06 '19 at 22:16
  • @Firestarter Even before you face [StaleElementReferenceException](https://stackoverflow.com/questions/53640973/staleelementexception-when-iterating-with-python/53646047#53646047) this line of code `driver.find_elements_by_class_name("select2 results__option")` will raise [Invalid selector: Compound class names not permitted](https://stackoverflow.com/questions/53528072/invalid-selector-compound-class-names-not-permitted-using-find-element-by-class/53536022#53536022) in the first place – undetected Selenium Jun 07 '19 at 08: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: https://stackoverflow.com/help/minimal-reproducible-example – Andrew Spencer Jun 07 '19 at 12:45

1 Answers1

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()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • `find_elements_by_class_name("select2 results__option")` ??? – undetected Selenium Jun 07 '19 at 08:05
  • @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. – KunduK Jun 07 '19 at 08:14
  • 1
    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](https://stackoverflow.com/questions/53528072/invalid-selector-compound-class-names-not-permitted-using-find-element-by-class/53536022#53536022) – undetected Selenium Jun 07 '19 at 08:17
  • Thank you! This worked but I had to change it to finding the elements by class name: – Firestarter Jun 07 '19 at 13:16
  • @Firestarter : Happy to help you. – KunduK Jun 07 '19 at 13:30