仿阿里巴巴行业门户贸易网站模板,WordPress十大免费CMS主题,重庆建工集团股份有限公司官网,延吉市建设局网站场景Wait类的使用场景是在页面上进行某些操作#xff0c;然后页面上就会出现或隐藏一些元素#xff0c;此时使用WebDriverWait类的until方法来等待这些效果完成以便进行后续的操作。另外页面加载时有可能会执行一些ajax#xff0c;这时候也需要去WebDriverWait的until的等待…场景Wait类的使用场景是在页面上进行某些操作然后页面上就会出现或隐藏一些元素此时使用WebDriverWait类的until方法来等待这些效果完成以便进行后续的操作。另外页面加载时有可能会执行一些ajax这时候也需要去WebDriverWait的until的等待ajax的请求执行完毕。具体一点的例子前面也曾出现过点击一个链接然后会出现一个下拉菜单此时需要先等待下拉菜单出现方可进行点击菜单项的操作。在实例化WebDriverWait类时有下面2个构造方法public WebDriverWait(WebDriver driver, longtimeOutInSeconds)public WebDriverWait(WebDriver driver, long timeOutInSeconds,long sleepInMillis)其参数为WebDriver driver。不言而喻long timeOutInSeconds。总体的超时时间最多等这么久。long sleepInMillis。每隔多久去检查一次until的结果另外要注意的是默认情况下unitl中的NotFoundException会被忽略但是其他异常还是正常传播的。你可以通过ignoring(exceptionsto add)自己定义需要忽略的异常。代码下面代码演示了点击按钮后如何等待label出现。这个例子其实没有前面的下拉菜单例子实用。wait.html html headmeta http-equivcontent-type contenttext/html;charsetutf-8/titlewaitscript typetext/javascript asyncsrchttp://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.jslinkhrefhttp://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.cssrelstylesheet /script typetext/javascript$(document).ready(function(){$(#btn).click(function(){$(waitr-webdriver).css(margin-top, 1em).insertAfter($(this));$(this).addClass(disabled).unbind(click);});});/script /head bodydiv classrow-fluid div classspan6 well h3wait button classbtnbtn-primary idbtn Click /div/div /body scriptsrchttp://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js /htmlwait.javaimportjava.io.File;importjava.util.List;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.JavascriptExecutor;importorg.openqa.selenium.Keys;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.chrome.ChromeDriver;importorg.openqa.selenium.support.ui.ExpectedCondition;importorg.openqa.selenium.support.ui.WebDriverWait;public class WaitExample{public static void main(String[] args) throwsInterruptedException {WebDriverdr new ChromeDriver();File file new File(src/wait.html);StringfilePath file:/// file.getAbsolutePath();System.out.printf(now accesss %s \n, filePath);dr.get(filePath);Thread.sleep(1000);// 点击按钮dr.findElement(By.id(btn)).click();(newWebDriverWait(dr, 10)).until(new ExpectedCondition() {public Boolean apply(WebDriver d) {returnd.findElement(By.className(label)).isDisplayed();}});Thread.sleep(1000);System.out.println(browser will be close);dr.quit();}}