你可以试试这个,看看它是否有效:
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");