【发布时间】:2015-12-15 03:38:11
【问题描述】:
我正在使用 Selenium 编写 Python 代码,它点击网页上的按钮。单击按钮后,按钮将变为其他内容。我的代码运行良好,但 Selenium 将其误解为一个问题。我试图忽略异常并继续前进,但我尝试过的任何方法都没有奏效。
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/fxdriver@googlecode.com/components/command-processor.js:8978)
at WebElement.getElementAttribute (file:///tmp/tmp24d8CK/extensions/fxdriver@googlecode.com/components/command-processor.js:12019)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp24d8CK/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp24d8CK/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at DelayedCommand.prototype.execute/< (file:///tmp/tmp24d8CK/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)
【问题讨论】:
-
您能否发布完整的回溯以及在哪一行引发错误?谢谢
-
@alecxe 感谢您的回复。我从控制台发布了信息,但我不知道这是否会有很大帮助。
-
感谢您的更新。但是,错误发生在代码的哪一行?
-
@alecxe 我在我的代码中发现了这个错误。很简单的错误。我单击按钮的那一行位于循环的末尾。该行实际上正确执行并返回到循环的顶部,那里发生了另一个过时的引用问题。不过感谢您的帮助!
-
当然,请将其作为答案发布并尽快接受以解决该主题。感谢分享。
标签: python selenium exception exception-handling