2015-12-15 178 views
1

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

你能发布完整的追踪和上线的错误被抛出?谢谢 – alecxe

+0

@alecxe感谢您的回复。我发布了来自控制台的信息,但我不知道这会有多大帮助。 –

+0

感谢您的更新。但是,在你的代码的哪一行发生错误? – alecxe

回答

1

我找到了答案,我的问题。

的位置代码:

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

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

1

这个问题是因为 HTML是加载和客户端仍然从服务器

接收更新我怎么做过对问题的解决方案,你可以简单地使用一下之前我的自定义等。如果你正在使用其他的浏览器之后,Chrome,然后调用滚动到该对象调用该函数

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

在此之后,这将解决你的问题代码

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); 
    } 
相关问题