java+selenium 時間等待


一、概述

       在我們 開發一腳本時,因為代碼的執行是非常快的,但瀏覽器的反應 往往需要一定時間 才能下一步  這樣代碼和瀏覽器不能同步會導致 報錯  代碼不穩定,為了更接近 用戶的方式操作瀏覽器 我們開發的代碼 往往在每一步執行 要等待瀏覽器反應過來在執行。

 

時間等待 有三種:強制等待、顯示等待、隱式等待

1、強制等待 

就是硬等待,使用方法Thread.sleep(int sleeptime),使用該方法會讓當前執行進程暫停一段時間(你設定的暫停時間)。弊端就是,你不能確定元素多久能加載完全,如果兩秒元素加載出來了,你用了30秒,造成腳本執行時間上的浪費, 代碼如下圖所示:

注:Thread.sleep() 單位是毫秒

 

import drive.DriveBrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class ById {


    public static void main(String[] args) throws InterruptedException {

        // 啟動瀏覽器
        WebDriver dr1 = new DriveBrowser().dr();

        // 打開百度
        dr1.get("http://www.baidu.com");

        // id元素定位到輸入框 搜索selenium
        dr1.findElement(By.id("kw")).sendKeys("selenium");

        // 等待3秒做下一步操作
        Thread.sleep(3000);



        // 點擊百度一下
        dr1.findElement(By.id("su")).click();


    }
}

  

2、顯示等待

  由WebDriver 提供 就是明確的要等到某個元素的出現或者是某個元素的可點擊等條件等到為止,才會繼續執行后續操作,等不到,就一直等,除非在規定的時間之內都沒找到,那么就拋出異常了

默認每500毫秒掃描界面是否出現元素、針對單一元素、可以設置超時時間

 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class Wait {


    public static void main(String[] args) {

        // 啟動瀏覽器
        WebDriver dr = new DriveBrowser().dr();
        // 打開百度網頁
        dr.get("http://wwww.baidu.com");

        // 顯示等待,針對某個元素等待
        WebDriverWait wa = new WebDriverWait(dr, 5, 1);
        wa.until(new ExpectedCondition<WebElement>() {
            //重寫方法
            @Override
            public WebElement apply(WebDriver text) {
                return text.findElement(By.id("kw"));
            }
        }).sendKeys("selenium");

        // 第二種寫法
        //wa.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("kw")));

        // 點擊百度一下
        dr.findElement(By.id("su")).click();
        // 強制等待3秒
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        dr.quit();


    }

 

3、隱式等待

 

WebDriver 提供了三種隱性等待方法:

 

  • implicitlyWait   識別對象時的超時時間。過了這個時間如果對象還沒找到的話就會拋出NoSuchElement 異常。

 

  • setScriptTimeout    異步腳本的超時時間。WebDriver 可以異步執行腳本,這個是設置異步執行腳本,腳本返回結果的超時時間。

 

  • pageLoadTimeout     頁面加載時的超時時間。因為 WebDriver 會等頁面加載完畢再進行后面的操作,所以如果頁面超過設置時間依然沒有加載完成,那么 WebDriver 就會拋出異常。 

 

隱式等待(implicit),方法implicitlyWait(long time, TimeUnit.SECONDS) 即全局設置,對整個driver都有作用,如在設定時間內,特定元素沒有加載完成,則拋出異常,如果元素加載完成,剩下的時間將不再等待。

 

 1     //隱式等待
 2     public void implicit(){
 3 
 4         WebDriver dr = new DriveBrowser().dr();
 5         //啟動瀏覽器
 6         dr.get("http://www.baidu.com");
 7 
 8         //頁面加載超時時間設置為 5s
 9         dr.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
10         dr.get("https://www.baidu.com/");
11 
12         //定位對象時給 10s 的時間, 如果 10s 內還定位不到則拋出異常
13         dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
14         dr.findElement(By.id("kw")).sendKeys("selenium");
15 
16         //異步腳本的超時時間設置成 3s
17         dr.manage().timeouts().setScriptTimeout(3, TimeUnit.SECONDS);
18 
19         dr.quit();
20 
21     }

 


免責聲明!

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



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