selenium1.0和selenium2.0頁面等待處理詳解


一、selenium1.0頁面等待

1、……AndWait

      經常會看到, selenium action命令中很多有這種……AndWait后綴, 例如click和clickAndWait命令:

click命令:點擊操作后, 直接進入下一個動作, 不做等待;

clickAndWait 命令,則是在click之后,自動執行一次waitForPageToLoad,等待直到當前窗口載入一個新頁面, 然而,這種等待只適合那些會引起頁面刷新的,如果頁面不是在當前窗口載入(比如是在一個彈出窗口,或者在一個iframe里載入),那么用AndWait是無法做到正確等待的,它會一直等待當前窗口的頁面載入直到超時,這種情況造成白白等待+timeout警告,另外常見的Ajax效果的界面, 使用……AndWait來等待也不起效

還有typeAndWait  selectAndWait等等

2、waitFor……

比較好的等待方式是使用waitFor……來等待需要的元素出現,例如,waitForElementPresent, 默認等待30秒, 在30秒內,如果元素已經出現, 馬上響應,否則報超時,可以通過IDE里options->options……中General tab來設置默認的timeout時間。

這里摘錄了下官網文檔中對 “不做等待”,AndWait, waitFor三種方式的闡述:

//1)The difference between a command and its AndWait alternative is that the regular command (e.g. click) will do the action and continue with the following command as fast as it can, while the AndWait alternative (e.g. clickAndWait) tells Selenium to wait for the page to load after the action has been done.
//2)The AndWait alternative is always used when the action causes the browser to navigate to another page or reload the present one.
//3)In AJAX driven web applications, data is retrieved from server without refreshing the page. Using andWait commands will not work as the page is not actually refreshed,,,The best approach would be to wait for the needed element in a dynamic period and then continue the execution as soon as the element is found.as waitForElementPresent orwaitForVisible


 

二、selenium2.0等待機制

1、顯性等待

第一種最簡單的方法是: Thread.sleep(1000)

這種設定了固定時間,若時間設置短了, 內容來不及更新, 設置長了延長了腳本運行時間,所以不推薦

第二種方式:采用 WebDriverWait類+ExpectedConditions接口,具體用法例子:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
       
WebElement e2=wait.until(new ExpectedCondition<WebElement>() {
     public WebElement apply(WebDriver d){
         return d.findElement(By.id("id locator"));
     }            
});

說明:這種方式類似seleniu1.0中的waitFor……, 上面代碼表示默認等待1-10s, 10內,若ExpectedConditions找到元素立刻返回,超過是10s報超時。

2、隱性等待

WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

說明:
執行driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);后

當要查找元素,而這個元素沒有馬上出現時,會告訴WebDriver查詢Dom一定時間(設置了10s)。默認值是0, 設置之后,這個時間將在WebDriver對象實例整個生命周期都起作用

 

三、附錄

這里列出常用的waitFor命令:

waitForElementPresent
locator
 
等待直到某個元素在頁面中存在
waitForElementNotPresent
locator
 
等待直到某個元素在頁面中不存在
 
 
waitForTextPresent
text
 
等待直到某個文本在頁面中存在
waitForTextNotPresent
text
 
等待直到某個文本在頁面中不存在
 
 
waitForText
locator
text
等待直到locator指定元素內的文本符合text
waitForNotText
locator
text
等待直到locator指定元素內的文本不符合text
 
 
waitForVisible
locator
 
等待直到locator指定元素在頁面中可見
waitForNotVisible
locator
 
等待直到locator指定元素在頁面中不可見
 
 
waitForTitle
title
 
等待直到頁面標題符合所指定的title
waitForNotTitle
title
 
等待直到頁面標題不符合所指定的title
 
 
waitForLocation
url
 
等待直到頁面的地址符合所指定的url
waitForNotLocation
url
 
等待直到頁面的地址不符合所指定的url
 
 
waitForValue
locator
value
等待直到locator指定的元素的value值符合指定的值
waitForNotValue
locator
value
等待直到locator指定的元素的value值不符合指定的值
 
 
waitForAttribute
value
等待直到locator指定的元素的attr屬性值符合指定的值
waitForNotAttribute
value
等待直到locator指定的元素的attr屬性值不符合指定的值
 
 
 
waitForChecked
locator
 
等待直到locator指定的radio或checkbox成為選中狀態
waitForNotChecked
locator
 
等待直到locator指定的radio或checkbox成為非選中狀態
 
 
 
waitForEditable
locator
 
等待直到locator指定的input或textarea元素可編輯
waitForNotEditable
locator
 
等待直到locator指定的input或textarea元素不可編輯
 
 
waitForXpathCount
xpath
count
等待直到頁面符合xpath的數量為count
waitForNotXpathCount
xpath
count
等待直到頁面符合xpath的數量不為count
 
 
waitForEval
script
pattern
等待直到script的執行結果符合pattern
waitForNotEval
script
pattern
等待直到script的執行結果不符合pattern

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM