我在使用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