1、硬性等待
Thread.sleep(1000);
2、隐式等待
在设置的超时时间范围内不断查找元素,直到找到元素或者超时
设置方式:driver.manage.timeouts().implicitlyWait(long time,TimeUnit unit);
例子:chromeDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
3、显示等待
用来等待某个条件发生后再继续执行后续代码(如找到元素、元素可点击、元素已显示等)
设置方式:
WebDriverWait wait=new WebDeriverWait();
WebElement element=wait.until(expectCondition);
例子:
WebDriverWait webDriverWait=new WebDriverWait(chromeDriver,5);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()=’_职业培训、考试提升在线教育平台’]")));
常用条件:
(1)页面元素在页面存在并可见
visibilityOfElementLocated(By locator)
(2)页面元素是否在页面上可用和可被单击
elementToBeClickable(By locator)
(3)页面元素处于被选中状态
elementToBeSelected(WebElement element)
(4)在页面元素中是否包含特定的文本
textToBePresentInElement(By locator)
(5)页面元素在页面中存在
presenceOfElementLocated(By locator)