我正在寻找类似于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秒之后就超时了。
你如何在你的代码中覆盖等待元素存在/可见?任何提示都是可以理解的。
发布于 2016-10-25 21:02:31
您可以使用显式等待或流畅等待
显式等待的示例-
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。
发布于 2017-07-24 15:20:48
我们在elementToBeClickable
上有很多竞争条件。参见https://github.com/angular/protractor/issues/2313。沿着这些思路的一些东西工作得相当好,即使是一点暴力
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)
);
发布于 2014-10-27 12:14:21
上面的wait语句是显式等待的一个很好的例子。
因为显式等待是受限于特定web元素的智能等待(如上面的x-path中所提到的)。
通过使用显式等待,你基本上是在最大限度上告诉WebDriver,在它放弃之前,它需要等待X个单位(无论你给出了什么timeoutInSeconds)。
https://stackoverflow.com/questions/11736027
复制相似问题