【问题标题】:Selenium WebDriver - handle StaleElementReferenceException (Python)Selenium WebDriver - 处理 StaleElementReferenceException (Python)
【发布时间】: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


【解决方案1】:

我找到了问题的答案。

这里的代码:

try:
    browser.find_element_by_xpath("//*[contains(text(), 'SomeButtonText')]").click()
except StaleElementReferenceException:
    pass

在 for 循环结束时执行。我完全错过了这样一个事实,即它返回到循环的开头,在那里我遇到了一个单独的问题,即 Stale 引用异常。

【讨论】:

    【解决方案2】:

    这个问题是因为 Html 正在加载,客户端仍在接收来自服务器的更新

    我如何解决问题,您只需在单击之前使用我的自定义等待。调用这个函数

     public static void waitForElementPresent(final By by, int timeout,WebDriver driver)
    

    在此之后,如果您使用的是 chrome 以外的浏览器,则调用 Scroll to that object 这将解决您的问题代码

    public static void waitForElementPresent(final By by, int timeout,WebDriver driver) { 
    
            waitForPageLoad(driver);
            WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,40).ignoring(StaleElementReferenceException.class); 
            /*  wait.until(new ExpectedCondition<Boolean>(){ 
                @Override 
                public Boolean apply(WebDriver webDriver) { 
                  WebElement element = webDriver.findElement(by); 
                  return element != null && element.isDisplayed(); 
                } 
              }); */
              try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
               wait.until(ExpectedConditions.presenceOfElementLocated(by));
              wait.until(ExpectedConditions.elementToBeClickable(by));
              WebDriverWait wait2 = new WebDriverWait(driver, 40);
              wait2.until(ExpectedConditions.elementToBeClickable(by));
    
            }
        //wait for page to laod 
        public static void waitForPageLoad(WebDriver driver) {
            ExpectedCondition<Boolean> pageLoadCondition = new
                ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver driver) {
                        return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                    }
                };
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(pageLoadCondition);
        }
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2011-06-18
      • 2020-04-25
      • 2018-09-14
      • 1970-01-01
      • 2012-04-24
      • 2014-03-03
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多