2017-10-13 49 views
0

我查看了關於在線論壇和本網站的討論並添加了等待功能。然而,它仍然顯示此錯誤 下面是該問題的代碼:測試拋出StaleElementReferenceException:元素不再附加到DOM或頁面已被刷新

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import com.google.common.base.Function; 

public class WebDriverNavigate { 
    public static void main(String[] args) throws InterruptedException { 
    System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"/GeckoDriver/geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 
     driver.navigate().to("http://www.google.com"); 
     WebElement textbox = driver.findElement(By.id("lst-ib")); 
     textbox.sendKeys("Search on Google");  
     WebDriverWait wait = new WebDriverWait(driver,10); 
     wait.until(visibilityOfElementLocated(By.id("lst-ib"))); 
     driver.findElement(By.name("btnK")).click();; 
     textbox.clear();     
     textbox.sendKeys("This is the second search"); 
     WebElement searchbutton2 = driver.findElement(By.id("fZl")); 
     searchbutton2.click(); 
     driver.navigate().back(); 
     driver.navigate().forward(); 
     driver.navigate().refresh(); 

    } 

    private static Function<WebDriver,WebElement> visibilityOfElementLocated(final By locator) { 
     return new Function<WebDriver, WebElement>() { 
      @Override 
      public WebElement apply(WebDriver driver) { 
       return driver.findElement(locator); 
      } 
     }; 
    } 
} 

下面是HTML代碼段:控制檯上

<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" value="Search on Google" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0%; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false" type="text"> 

錯誤消息:

線程「main」中的異常org.openqa.selenium.StaleElementReferenceException:stal的元素引用e:元素不再附加到DOM或頁面已被刷新

回答

0

我能夠在這個順序重新排列的代碼來解決這個問題:

 WebElement searchbutton = driver.findElement(By.name("btnK")); 
     driver.findElement(By.name("btnK")).click(); 
     WebDriverWait wait = new WebDriverWait(driver,10); 
     WebElement text2 = wait.until(visibilityOfElementLocated(By.id("lst-ib"))); 
     text2.clear(); 

它看起來像我嘗試在click()之前等待,但應該等待在click()之後但在clear()之前加載該元素。現在它不會拋出該錯誤

T向你提供所有建議和解決方案。我相信他們在將來會遇到更復雜的代碼問題。

0

我有這個問題多次,有時它是不可能解決的。

首先,您應該找到具有Wait或WaitFluent硒類的元素。因爲也許你試着在元素尚未加載之前找到元素。類似的東西:

new FluentWait<WebDriver>(driver) 
      .withTimeout(IMPLICIT_TIMEOUT, TimeUnit.SECONDS) 
      .pollingEvery(RETRY_TIME, TimeUnit.SECONDS) 
      .ignoring(StaleElementReferenceException.class, 
         NoSuchElementException.class) 
      .until(ExpectedConditions 
      .visibilityOfElementLocated(By.xpath(xpath))); 

其他的方法是嘗試多次在一個與一個driver.sleep()裏面。

+0

謝謝!看來我的代碼被錯誤地重新安排了。我已經回答了我如何解決這個特定問題。不過,我知道你的建議在未來遇到類似問題時很有用。 –

+0

歡迎您。如果它們有用,你可以投票回答。 –

0

大多數情況下,這是由於DOM正在更新並且您嘗試訪問更新/新元素。所以this Question&Answers應該涵蓋你。您也可以檢查Selenium Documentation

在大多數情況下explicit wait的作品。

,或者你可以忽略的錯誤,直到該元素可以點擊

new WebDriverWait(driver, timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))); 
driver.findElement(By.name("btnK")).click(); 
+0

我試過了你提供的代碼。但是,它仍然顯示相同的錯誤。我將嘗試查看* Questions&Answers *中的其他解決方案是否有效。謝謝 –

+0

謝謝!看來我的代碼被錯誤地重新安排了。我已經回答了我如何解決這個特定問題。不過,我知道你的建議在未來遇到類似問題時很有用。 –

相關問題