首页 > 技术文章 > selenium+java的几种等待

silna 2018-08-24 09:38 原文

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

 

推荐阅读