Webdriver同步設置常用等待類主要如下圖所示
?
注:support.ui包內類主要實現顯性等待功能,timeouts()內方法主要實現隱性等待功能
一.線程休眠?
Thread.sleep(
long
millis)
?
二.隱形等待
? 隱性等待:設置一次,driver整個生命周期中都在使用,不需要針對元素明確設置
driver.manage().timeouts().implicitlyWait(
long
outTime, TimeUnit unit);
全局設置,設置driver執行所有定位元素操作的超時時間
Parameters: outTime – 超時等待時間; unit – 時間單位.
Ex.?
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 超時等待時間為10S
?
?
?
三.顯性等待
?
????顯性等待:需要等待的元素定位時需要逐一明確調用該等待方法
Figure 1 Support.ui包常用類UML
?
[ 常用等待模板 ]
-
等待元素加載
1)WebDriverWait
new WebDriverWait(WebDriver driver, long timeOutInSeconds).until(ExpectedConditions.presenceOfElementLocated(By by))
注解 :
-
Parameters : driver-webdriver; timeOutInSeconds- 超時 等待時間( s ) ; by- 元素 locator
-
timeOutInSeconds 范圍內,等待滿足 until() 方法中的條件 ,即刻跳出等待。
-
超出 timeOutInSeconds , 拋出異常.
? 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));
注解 :
-
Parameters : driver-webdriver; timeOut - 超時等待時間( s ) ; unit-時間單位; by- 元素 locator;
duration-查找元素時間間隔 ; exceptionType-忽略異常類型,例如 默認NoSuchElementException.class
-
timeOut 范圍內, driver 每隔 dration 定位一次元素,若遇到 exceptionType 則繼續等待,直到滿足 until() 方法中的條件 ,即刻跳出等待。
-
超出 timeOutInSeconds 仍 未滿足until條件 , 拋出異常.
? 2.等待并獲取元素—僅需要修改 until 方法體
?
WebElement element = wait.until( new ExpectedCondition<WebElement> (){ @Override public WebElement apply( WebDriver driver) { return driver.findElement( By by); } } );
注解 :
- Parameters : driver-webdriver , 從 W ebDriverWait|FluentWait 實例 化方法中獲取 ; by- 元素 locator
- Return: 若定位到元素,返回webelement;否則報異常
[ 案例 ]
-
等待頁面元素加載
- WebDriverWait
Wait<WebDriver> wait = new WebDriverWait(driver,10 ); wait.until( ExpectedConditions. presenceOfElementLocated(By.id( "myDynamicElement" )) );
方法功能:定位 id=' myDynamicElement' 的元素,超時等待時間為 10S
-
- FluentWait
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 。
-
等待并獲取頁面元素
- WebDriverWait
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 ,若定位到元素,直接返回元素
-
- ? FluentWait
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, 兩種方式都可以 , 只是第一種方式還需要再多一步獲取的操作 .
四.Wait
FluentWait類是Wait接口的實現,直接使用wait接口可以更靈活的完成您需要的等待功能,例如
Wait w = new Wait(){ @Override public boolean until() { return webElement.isDisplayed(); } };
這種等待的方式 , 據說在加載 js 代碼的時候做判斷會比較方便 , 目前沒有調試成功。
?五、其他
1.等待頁面加載
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);
注解:
-
pageloadTimeout方法只適用于firefox瀏覽器,Chrome等其他瀏覽器并不適用,但我們可以自己實現一個定時器
-
該方法設置頁面加載超時時間為100s
?
?
driver.manage().timeouts().setScriptTimeout(100,SECONDS);
尚未調試通過
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
