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!