Thread.sleep(long millis)
隐性等待:设置一次,driver整个生命周期中都在使用,不须要针对元素明确设置web
driver.manage().timeouts().implicitlyWait(long outTime, TimeUnit unit);
全局设置,设置driver执行全部定位元素操做的超时时间 浏览器
Parameters:outTime– 超时等待时间;unit– 时间单位.app
Ex. 异步
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//超时等待时间为10S
显性等待:须要等待的元素定位时须要逐一明确调用该等待方法 ide
Figure 1 Support.ui包经常使用类UMLui
[经常使用等待模板] spa
1)WebDriverWait firefox
new WebDriverWait(WebDriver driver, long timeOutInSeconds).until(ExpectedConditions.presenceOfElementLocated(By by))
注解:线程
2)FluentWait 调试
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(long timeOut, TimeUnit unit) .pollingEvery(long duration, TimeUnit unit) .ignoring(exceptionType); wait.until(ExpectedConditions.presenceOfElementLocated(By.by));
注解:
duration-查找元素时间间隔 ;exceptionType-忽略异常类型,例如 默认NoSuchElementException.class
2.等待并获取元素—仅须要修改until方法体
WebElement element = wait.until( new ExpectedCondition<WebElement>(){ @Override public WebElement apply( WebDriver driver) { return driver.findElement( By by); } } );
注解:
[案例]
Wait<WebDriver> wait = new WebDriverWait(driver,10); wait.until( ExpectedConditions. presenceOfElementLocated(By.id("myDynamicElement")) );
方法功能:定位id=' myDynamicElement'的元素,超时等待时间为10S
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(60, TimeUnit.SECONDS) .pollingEvery(10, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); wait.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
方法功能:定位id=' myDynamicElement'的元素,超时等待时间为60S,每隔10S定位一次,遇到NoSuchElementException报错忽略,继续等待直到超过60s。
Wait<WebDriver> wait = new WebDriverWait(driver, 10); WebElement e = wait.until( new ExpectedCondition< WebElement>(){ @Override public WebElement apply( WebDriver d) { return d.findElement( By.id( " myDynamicElement " )); } } );
方法功能:定位id=' myDynamicElement'的元素,超时等待时间为10S,若定位到元素,直接返回元素
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(60, TimeUnit.SECONDS) .pollingEvery(10, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); WebElement element= wait.until( new ExpectedCondition<WebElement>() { @Override public WebElement apply( WebDriver d) { return d.findElement( By.id( " myDynamicElement " )); } } );
方法功能:定位id=' myDynamicElement'的元素,超时等待时间为60S,每隔10S定位一次,60s内遇到NoSuchElementException报错忽略,继续等待;若定位到元素,直接返回该页面元素。
若是只是仅仅想判断页面是否是加载到某个地方了,就能够用第一种方法; 但若是须要获得某个WebElement,两种方式均可以,只是第一种方式还须要再多一步获取的操做.
FluentWait类是Wait接口的实现,直接使用wait接口能够更灵活的完成您须要的等待功能,例如
Wait w = new Wait(){ @Override public boolean until() { return webElement.isDisplayed(); } };
这种等待的方式,听说在加载js代码的时候作判断会比较方便,目前没有调试成功。
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
注解:
driver.manage().timeouts().setScriptTimeout(100,SECONDS);
还没有调试经过