我正在寻找类似于waitForElementPresent检查元素是否在我点击之前显示的东西.我认为这可以通过implicitWait,所以我使用以下内容:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
然后单击
driver.findElement(By.id(prop.getProperty(vName))).click();
Run Code Online (Sandbox Code Playgroud)
不幸的是,有时它等待元素,有时候不等.我找了一会儿找到了这个解决方案:
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();
Run Code Online (Sandbox Code Playgroud)
它等了很久,但在超时前它必须等待5次,50秒.有点多.所以我把隐式等待设置为1秒,直到现在一切都好了.因为现在有些事情在超时之前等待10秒,但是其他一些事情在1秒之后会超时.
您如何覆盖代码中存在/可见元素的等待?任何提示都很明显.
我正在尝试让Selenium等待页面加载后动态添加到DOM的元素.试过这个:
fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId"));
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,这里是fluentWait:
FluentWait fluentWait = new FluentWait<>(webDriver) {
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(200, TimeUnit.MILLISECONDS);
}
Run Code Online (Sandbox Code Playgroud)
但它会抛出NoSuchElementException- 看起来像presenceOfElement期望元素存在,所以这是有缺陷的.这对于Selenium来说一定是面包和黄油,并且不想重新发明轮子......任何人都可以建议一个替代品,理想情况下不要自己动手Predicate吗?