2015-12-15 163 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); 
    } 
相關問題