WebDriverWait,配合该类的until()和until_not()方法,就可以根据判断条件而进行灵活地等待了。app
程序每隔xx秒看一眼,若是条件成立了,则执行下一步,不然继续等待,直到超过设置的最长时间,而后抛出TimeoutException。ide
//页面元素是否在页面上可用和可被点击 ExpectedConditions.elementToBeClickable(By locator); //页面元素是否处于被选中状态 ExpectedConditions.elementToBeSelected(By locator); //页面元素在页面是否存在 ExpectedConditions.presenceOfElementLocated(By locator); //是否包含特定的文本 ExpectedConditions.textToBePresentInElement(locator, text) //页面元素值 ExpectedConditions.textToBePresentInElementValue(locator, text); //标题 ExpectedConditions.titleContains(title);
public static void sendKeysByXPath(WebDriver driver, String path, String key) { WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒 WebElement element = wait.until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver d) { return d.findElement(By.xpath(path)); } }); highLightElement(driver,element); element.clear(); element.sendKeys(key); }
FluentWait:能够动态设置巡检时间spa
//FluentWait Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(60, TimeUnit.SECONDS) .pollingEvery(2, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement ele1 = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("xxxxxxx")); } });
设置超时时间,等待页面加载,若是在规定时间加载完成就执行下一步;code
弊端:须要等整个页面加载完成,才能执行下一步对象
周期:对driver整个周期都适用,执行一次便可blog
pageLoadTimeout.页面加载超时时间ci
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Thread.sleep(2000);