1.隐形等待
隐形等待:隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
2.. 显性等待
显式等待 使用ExpectedConditions(期望条件)类中自带方法, 可以进行显试等待的判断。 只有满足显式等待的条件满足,代码才会继续向后执行,如果超过设定的最大显式等待时间, 这程序会抛出异常。
定义的等待条件可以有:
(1)页面元素是否在页面上可用和可被单击:elementToBeClickable(By locator)
(2)页面元素处于被选中状态:elementToBeSelected(WebElement element)
(3)页面元素在页面中存在:presenceOfElementLocated(By locator)
(4)在页面元素中是否包含特定的文本:textToBePresentInElement(By locator)
(5)页面元素值:textToBePresentInElementValue(By locator, java.lang.String text)
(6)标题 (title):titleContains(java.lang.String title)
(7)元素可见(加载出现):visibilityOfElementLocated()
(8)元素不可见(加载消失):invisibilityOfElementLocated()
1.等待条件:判断页面元素是否存在:presenceOfElementLocated()
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.presenceOfElementLocated(By.***("******")));
2.元素加载出现然后再消失
new WebDriverWait(driver, TIME_OUT_SECONDS)
.until(ExpectedConditions.visibilityOfElementLocated(By.id("*****")));
//------------------
new WebDriverWait(driver, TIME_OUT_SECONDS)
.until(ExpectedConditions.invisibilityOfElementLocated(By.id("******")));