我在使用Selenium编写Python代码,点击网页上的按钮。单击按钮后,该按钮将变为其他内容。我的代码工作得很好,但Selenium将这误解为一个问题。我试图忽略这个异常并继续前进,但我没有尝试过任何工作。Selenium WebDriver - 处理StaleElementReferenceException(Python)
from selenium.common.exceptions import StaleElementReferenceException
try:
browser.find_element_by_xpath("//*[contains(text(), 'SomeButtonText')]").click()
except StaleElementReferenceException:
pass
如何忽略异常并继续前进?我的代码与我正在尝试做的事情一起工作,但在几秒钟后,抛出异常。
Message: Element not found in the cache - perhaps the page has changed since it was looked up
这里是Ubuntu的终端非常杂乱信息:
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)
你能发布完整的追踪和上线的错误被抛出?谢谢 – alecxe
@alecxe感谢您的回复。我发布了来自控制台的信息,但我不知道这会有多大帮助。 –
感谢您的更新。但是,在你的代码的哪一行发生错误? – alecxe