WebDriver-使用Java等待元素
我正在寻找类似于WebDriver-使用Java等待元素,java,webdriver,selenium-webdriver,Java,Webdriver,Selenium Webdriver,我正在寻找类似于waitForElementPresent的内容,以在单击元素之前检查元素是否显示。我认为这可以通过implicitWait实现,因此我使用了以下方法: driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 然后点击 driver.findElement(By.id(prop.getProperty(vName))).click(); 不幸的是,它有时等待元素,有时不等待。我找了一会儿,找到了这个解决方
waitForElementPresent
的内容,以在单击元素之前检查元素是否显示。我认为这可以通过implicitWait
实现,因此我使用了以下方法:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
然后点击
driver.findElement(By.id(prop.getProperty(vName))).click();
不幸的是,它有时等待元素,有时不等待。我找了一会儿,找到了这个解决方案:
for (int second = 0;; second++) {
Thread.sleep(sleepTime);
if (second >= 10)
fail("timeout : " + vName);
try {
if (driver.findElement(By.id(prop.getProperty(vName))).isDisplayed())
break;
} catch (Exception e) {
writeToExcel("data.xls", e.toString(), parameters.currentTestRow, 46);
}
}
driver.findElement(By.id(prop.getProperty(vName))).click();
它等待的很好,但在超时之前,它必须等待10次5,50秒。有点多。因此,我将隐式等待设置为1秒,直到现在一切似乎都很好。因为现在有些东西在超时前等待10秒,而有些东西在1秒后超时
如何涵盖代码中存在/可见的等待元素?任何提示都是值得注意的。这就是我在代码中的做法
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
WebDriverWait wait=new-WebDriverWait(webDriver,timeoutingseconds);
等待.直到(预期条件.元素的可视性(通过.id));
或
wait.until(ExpectedConditions.elementtobelickable(By.id));
准确地说
另见:
- 为各种等待场景提供类似的快捷方式
- 对于它的各种构造函数
上面的wait语句是显式等待的一个很好的例子 As显式等待是限制在特定web元素中的智能等待(如上面的x-path中所述)
通过使用显式等待,您基本上是告诉WebDriver,在放弃之前,最多要等待X个单位的时间(无论您给出的时间是多少秒)。您可以使用显式等待或流畅的等待 显式等待示例-
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));
Fluent等待示例-
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"));
}
});
Wait Wait=new FluentWait(驱动程序)
.带超时(20,时间单位。秒)
.轮询间隔(5,时间单位。秒)
.忽略(NoSuchElementException.class);
WebElement aboutMe=wait.until(新函数(){
公共WebElement应用(WebDriver驱动程序){
返回driver.findElement(By.id(“about_me”);
}
});
查看此项以了解更多详细信息。我们有很多可选择的
元素的比赛条件。看见即使有一点蛮力,沿着这条路线的一些东西也相当有效
Awaitility.await()
.atMost(timeout)
.ignoreException(NoSuchElementException.class)
.ignoreExceptionsMatching(
Matchers.allOf(
Matchers.instanceOf(WebDriverException.class),
Matchers.hasProperty(
"message",
Matchers.containsString("is not clickable at point")
)
)
).until(
() -> {
this.driver.findElement(locator).click();
return true;
},
Matchers.is(true)
);
谢谢如果我能更早地了解这个类,我的生活会更轻松:)我如何将您的代码合并到这个格式中?@FindBy(how=how.ID,使用=“注册按钮”)WebElement signUpButton代码>此外,我仍然可以用你的代码获得NPE。看起来它正试图让Element变得可访问。当元素未加载时,我们如何使用此方法?在您的答案中添加一些代码片段,因为其他用户可能会对答案进行不同的排序,并且“上面”的上下文可能会发生变化。已经5年了,并且仍在等待代码片段,以便对其他正在搜索该主题信息的人有意义。如果有人想了解有关此答案的更多信息。此方法已弃用。
Awaitility.await()
.atMost(timeout)
.ignoreException(NoSuchElementException.class)
.ignoreExceptionsMatching(
Matchers.allOf(
Matchers.instanceOf(WebDriverException.class),
Matchers.hasProperty(
"message",
Matchers.containsString("is not clickable at point")
)
)
).until(
() -> {
this.driver.findElement(locator).click();
return true;
},
Matchers.is(true)
);