-1

Junit 4 と selenium WebDriver と Eclipse を使用して、ubuntu でテストを実行しています。
テストを実行すると、次のエラーが発生します。

> org.openqa.selenium.StaleElementReferenceException: Element not found
> in the cache - perhaps the page has changed since it was looked up

そして、テストをデバッグすると動作します。
そして、これは私のテストです:

package com.QD2.Login;

import java.util.concurrent.TimeUnit;   
import org.junit.*;   
import static org.junit.Assert.*;   
import org.openqa.selenium.*;  
import org.openqa.selenium.firefox.FirefoxDriver;

public class Login {    

  private WebDriver driver;    
  private String baseUrl;    
  private StringBuffer verificationErrors = new StringBuffer();

  @Before   public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8088/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);   
  } 

  @Test   public void testUntitled() throws Exception {
    driver.get(baseUrl + "/QD2/pages/accueil/login.xhtml#loaded");
    //driver.findElement(By.id("login")).clear();
    driver.findElement(By.xpath("//*[@id='login']")).sendKeys("admin");
    //driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys("admin");
    driver.findElement(By.id("loginButton")).click();   
  }

  @After   public void tearDown() throws Exception {
    //driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }   
  }
}
4

1 に答える 1

0

これは次の例です:org.openqa.selenium.StaleElementReferenceException

WebElement element = driver.findElement(By.id("input123")); // get the input 
WebElement button= driver.findElement(By.id("btn123")); // this button sends the form
button.click();
element.sendKeys("the cache is already released.."); // error here

だから私はこれが#loadedURLの中にあるあなたから来ていると思います

driver.get(baseUrl + "/QD2/pages/accueil/login.xhtml#loaded");

それなしで試してみて、何が起きているのか教えてください。


アップデート :

次のような待機を使用してみてください:

[...]
WebElement login = (new WebDriverWait(driver, 10))
              .until(ExpectedConditions.presenceOfElementLocated(By.id("login")));
login.sendKeys("admin");
[...]

ただし、別のページに移動してソースを変更したり、フォームを送信したりしたときにこのエラーが発生するため、通常は待機は役に立ちません。

于 2013-05-22T09:23:30.317 に答える