Selenium_等待頁面加載完畢


隱式等待

WebDriver driver = new FirefoxDriver();
        driver.get("www.baidu.com");    
        
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebElement element = driver.findElement(By.cssSelector(".abc"));      
        ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);

注:隱式等待設置的內容在driver的整個生命周期都有效,所以實際使用過程當中有弊端。

  等待20秒元素還不存在,就會拋出異常  org.openqa.selenium.NoSuchElementException

顯式等待

顯式等待 使用ExpectedConditions類中自帶方法, 可以進行顯試等待的判斷。 

顯式等待可以自定義等待的條件,用於更加復雜的頁面等待條件

 

等待的條件

WebDriver方法

頁面元素是否在頁面上可用和可被單擊

elementToBeClickable(By locator)

頁面元素處於被選中狀態

elementToBeSelected(WebElement element)

頁面元素在頁面中存在

presenceOfElementLocated(By locator)

在頁面元素中是否包含特定的文本

textToBePresentInElement(By locator)

頁面元素值

textToBePresentInElementValue(By locator, java.lang.String text)

標題 (title)

titleContains(java.lang.String title)

 

 

 

 

 

 

 

 

 

 

 

 

 

只有滿足顯式等待的條件滿足,測試代碼才會繼續向后執行后續的測試邏輯

如果超過設定的最大顯式等待時間閾值, 這測試程序會拋出異常。

public static void testWait2(WebDriver driver)
    {
        driver.get("www.baidu.com");    
        
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".abc")));
        WebElement element = driver.findElement(By.cssSelector(".abc"));      
        ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);  
    }

 


免責聲明!

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



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