我正在使用HtmlUnitDriver定位一个元素。元素是文本字段(输入)。
WebElement username = driver.findElement(By.id("username"));
username.clear();
username.sendKeys("myValue");
我尝试使用 username.clear() 清除其内容。但这给了我
元素似乎已过时。您是否离开了包含它的页面?
这没什么意义,因为我不会在页面上移动。有人知道这是什么吗?
你可以试试这个,看看它是否有效:
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");