(java)selenium webdriver學習---三種等待時間方法:顯式等待,隱式等待,強制等待


selenium webdriver學習---三種等待時間方法:顯式等待,隱式等待,強制等待

本例包括窗口最大化,刷新,切換到指定窗口,后退,前進,獲取當前窗口url等操作;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * 方法3,隱式等待,隱式等待相當於設置全局的等待,在定位元素時,對所有元素設置超時時間。
 * 隱式等待使得WebDriver在查找一個Element或者Element數組時,每隔一段特定的時間就會輪詢一次DOM,
 * 如果Element或數組沒有馬上被發現的話。默認設置是0。一旦設置,這個隱式等待會在WebDriver對象實例的整個生命周期起作用。一勞永逸。
 * ********
 * 方法1,強制等待,固定休眠時間設置,Java的Thread類里提供了休眠方法sleep,導入包后就能使用,
 * 執行到此時不管什么就固定的等待5秒之后再接着執行后面的操作
 * ********
 * 方法2,顯式等待,在0-10s中去定位id為“su”的元素,WebDriverWait默認每500ms就調用一次ExpectedCondition
 * 直到定位成功或者時間截止,ExpectedCondition的返回值要么為true要么為不為空的對象,
 * 在規定時間內若沒有定位成功元素,則until()會拋出org.openqa.selenium.TimeoutException 。*/

public class YsfTest_20180726{
    private static final int ExpectedCondition = 0;
    private static final int Boolean = 0;

    public static void main(String[] args) throws InterruptedException{
        WebElement search = null;
        System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        //方法3,隱式等待,implicitlyWait()方法比sleep()方法智能,sleep()方法只能在一個固定的時間等待,而implicitlyWait()可以在一個時間范圍內等待,稱為隱式等待
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com/");
        //窗口最大化
        driver.manage().window().maximize();
        //定位所搜框,並輸入電影
        driver.findElement(By.cssSelector("#kw")).sendKeys("電影");
        //方法1,強制等待,固定休眠時間設置,sleep()方法以毫秒為單位 ,此類為設置5s等待
        Thread.sleep(5000);
        //定位百度一下按鈕
        WebElement searchButton = driver.findElement(By.cssSelector("#su"));
        //點擊百度一下
        searchButton.submit();
        //獲得當前窗口url
        String parentUrl = driver.getCurrentUrl();
        System.out.println("currenturl:"+parentUrl);
        //獲取搜索navigate窗口
        driver.navigate().to("https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=navigate");
        //方法2,顯式等待
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>(){
            public Boolean apply(WebDriver d){
                return d.getTitle().toLowerCase().startsWith("navigate");
            }
        });
        //獲得當前窗口url
        String parentUrl2 = driver.getCurrentUrl();
        System.out.println("currenturl:"+parentUrl2);
        //刷新頁面
        driver.navigate().refresh();
        //后退到百度首頁
        driver.navigate().back();
        //方法2,顯式等待,在規定時間內等待 在10秒的范圍內 出現.red_box元素就往下執行,如果10秒過后還沒出現就跳出
        WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
        //前進到搜索navigate窗口
        driver.navigate().forward();
        driver.close();
    } 
    
}

 

注意,如果顯式等待搜索的內容不存在,則會跑出異常;


免責聲明!

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



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