元素似乎是webelement.clear()上的陈旧错误

时间:2013-01-30 14:01:46

标签: selenium junit

我正在使用HtmlUnitDriver找到一个元素。元素是文本字段(输入)。

    WebElement username = driver.findElement(By.id("username"));
    username.clear();
    username.sendKeys("myValue");

我尝试用username.clear()清除其内容。但这给了我

元素似乎陈旧。您是否离开了包含它的页面?

没什么意义,因为我没有在页面上移动。谁知道这是什么?

1 个答案:

答案 0 :(得分:1)

你可以尝试一下,看看它是否有效:

public static WebElement getElementByLocator( By locator ) {
  driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
  WebElement we = null;
  boolean unfound = true;
  while ( unfound ) {
    try {
        we = driver.findElement( locator );
        unfound = false; // FOUND IT
      } catch ( StaleElementReferenceException e ) {                        
        unfound = true;
        Thread.sleep(4000);
      }     
    } 
  }
  driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, 
        TimeUnit.SECONDS );
  return we;
}

然后,

WebElement username = getElementByLocator( By.id("username") );
username.clear();
username.sendKeys("myValue");