好吧,有两种类型的等待:显式和隐式等待。显式等待的想法是
WebDriverWait.until(condition-that-finds-the-element);
隐式等待的概念是
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
你可以在此处获得细节上的差异。
在这种情况下,我宁愿使用显式等待(
fluentWait尤其是):
public WebElement fluentWait(final By locator) { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } }); return foo;};fluentWait函数返回找到的Web元素。从文档开始
fluentWait: 等待接口的实现,可以动态配置其超时和轮询间隔。每个
FluentWait实例都定义了等待条件的最长时间,以及检查条件的频率。此外,用户可以配置等待以在等待时忽略特定类型的异常,例如在页面上搜索元素时的
NoSuchElementExceptions。 你可以在这里找到详细信息
fluentWait你的情况的用法如下:
WebElement textbox = fluentWait(By.id("textbox"));恕我直言,这种方法更好,因为你不确切知道要等待多少时间,并且可以在轮询间隔中设置任意时间值,以验证哪个元素存在。问候。



