我正在为使用 selenium chrome 驱动程序编写一些自动化测试。我试图编写一个可重用的方法,该方法将显式等待元素出现,然后在其他类中调用此方法。看起来很简单,但它没有按照我的意愿去做。这是我的方法。
public String waitForElement(String item) {
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(By.id(item)));
return item;
}
然后我调用该方法并向其传递一个参数,如下所示:
waitForElement("new-message-button");
这似乎行不通,有人可以提供一些见解吗?
最佳答案
您可以使用显式等待或流畅等待
显式等待示例 -
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));
流畅等待示例 -
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(20, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("about_me"));
}
});
检查这个TUTORIAL了解更多详情。
关于java - Selenium webdriver 显式等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20077860/