I am writing Python code using Selenium which clicks on a button on a web page. After the button is clicked, the button changes to something else. My code works perfectly fine, but Selenium is misinterpreting this as a problem. I'm attempting to ignore the exception and move on but nothing I've tried has worked.
from selenium.common.exceptions import StaleElementReferenceException
try:
browser.find_element_by_xpath("//*[contains(text(), 'SomeButtonText')]").click()
except StaleElementReferenceException:
pass
What can I do to ignore the exception and move on? My code WORKS with what I'm trying to do, but after a couple seconds, the exception is thrown.
Message: Element not found in the cache - perhaps the page has changed since it was looked up
Here is the extremely messy information from the Ubuntu Terminal:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 107, in get_attribute
resp = self._execute(Command.GET_ELEMENT_ATTRIBUTE, {'name': name})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 454, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9351)
at Utils.getElementAt (file:///tmp/tmp24d8CK/extensions/[email protected]/components/command-processor.js:8978)
at WebElement.getElementAttribute (file:///tmp/tmp24d8CK/extensions/[email protected]/components/command-processor.js:12019)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp24d8CK/extensions/[email protected]/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp24d8CK/extensions/[email protected]/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmp24d8CK/extensions/[email protected]/components/command-processor.js:12481)