2017-10-13 100 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

谢谢!看来我的代码被错误地重新安排了。我已经回答了我如何解决这个特定问题。不过,我知道你的建议在未来遇到类似问题时很有用。 –

相关问题