我正在寻找类似于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>));

要么
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));

确切地说。

也可以看看:
  • org.openqa.selenium.support.ui.ExpectedConditions用于各种等待场景的类似快捷方式。
  • org.openqa.selenium.support.ui.WebDriverWait用于其各种构造函数。
  • 10-07 18:54
    查看更多