selenium+java的幾種等待


1.隱式等待

WebDriver driver = new FirefoxDriver();
        driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html");    
        
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebElement element = driver.findElement(By.cssSelector(".red_box"));      
        ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);  

driver.manage().timeouts().implicitlyWait是全局等待設置,即所有命令執行時都會給予設定的等待時長

driver.manage().timeouts().pageLoadTimeout是頁面加載等待

2.顯示等待

顯示等待是使用ExceptionConditions里面的方法

等待的條件

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)

 

 

 

 

 

 

 

 

 

 

 

 

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

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

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

    public static void testWait2(WebDriver driver)
    {
        driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦東軟件園培訓中心\\我的教材\\Selenium Webdriver\\set_timeout.html");    
        
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
        WebElement element = driver.findElement(By.cssSelector(".red_box"));      
        ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);  
    }

3.線程休眠,直接等待

Thread.sleep(1000);///線程休眠1秒

 

 

參考地址:https://www.cnblogs.com/TankXiao/p/5246557.html

 


免責聲明!

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



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