【Selenium】9显示等待、隐式等待

本文介绍 WebDriver 中显示等待与隐式等待的概念及使用方法。覆盖 WebDriverWait 的配置、ExpectedConditions 的各种等待条件,如元素可见、可点击等,并探讨了如何自定义等待条件。此外,还提供了 Ajax 请求完成及元素存在的判断示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

显示等待

WebDriverWait

elementToBeCllickable(By locator)页面元素是否出现可用(enabled)和可被点击  
elementToBeSelected(WebElement element)页面元素处于被选中状态   
presenceOfElementLocated(By locator)页面元素在页面中存在
textToBePresentInElement(By locator)在页面元素中是否包含特定的文本
textToBePresentInElementValue(By locator,java.lang.Stringtext)页面元素值
titleContains(java.lang.String.title)标题


WebDriverWait wait=new WebDriverWait(driver,10);
//获取内容出现
wait.until(ExpectedConditions.titleContains("baoyu"));
System.out.println("baoyu出现了");
//列表选项是否选中
WebElement element = driver.findElement(By.id(""));
wait.until(ExpectedConditions.elementToBeSelected(element));
//列表选项是否可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
//是否在页面中
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("")));
//是否包含文字
WebElement p= driver.findElement(By.id(""));
wait.until(ExpectedConditions.textToBePresentInElement(p,"爱吃水果"));

超时抛出TimeOutException,默认500毫秒

public class WaitToReturnElement {

   /*
   * 设置超时时间为5秒,返回指定xpath的WebElement
   * */
   public static WebElement waitForByXpath(final WebDriver driver,final String xpath) {
      WebDriverWait wait = new WebDriverWait(driver, 5);
      return wait.until(new ExpectedCondition<WebElement>() {
         
         public WebElement apply(WebDriver arg0) {
            return driver.findElement(By.xpath(xpath));
         }
         
      });
   }

   /*
   * 设置超时时间为10秒,返回指定id的WebElement
   * */
   public static WebElement waitForById(final WebDriver driver,final String id) {
      WebDriverWait wait = new WebDriverWait(driver, 10);
      return wait.until(new ExpectedCondition<WebElement>() {
         
         public WebElement apply(WebDriver arg0) {
            return driver.findElement(By.id(id));
         }
         
      });
   }
   
   /*
   * 设置超时时间为10秒,返回指定xpath的WebElement是否出现
   * */
   public static Boolean isElementDisplayed(final WebDriver driver,final String xpath) {
      WebDriverWait wait = new WebDriverWait(driver, 10);
      return wait.until(new ExpectedCondition<Boolean>() {
         
         public Boolean apply(WebDriver arg0) {
            return driver.findElement(By.xpath(xpath)).isDisplayed();
         }        
      });
   }
   
}
//判断Ajax请求是否加载完成
public static Boolean ajaxRequestFinish(final WebDriver driver) {

   WebDriverWait wait = new WebDriverWait(driver, 10);
   return wait.until(new ExpectedCondition<Boolean>() {

      public Boolean apply(WebDriver arg0) {
         JavascriptExecutor js = (JavascriptExecutor) driver;
         return (Boolean) js.executeScript("return jQuery.active==0");
      }
   });
}

//判断页面元素是否存在
WebDriver driver;
    
    private Boolean IsElementPresent(By by){
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return  false;
        }

    }
    @Test

public void testsElementPresent(){
        driver.get("");
        if(IsElementPresent(By.id(""))){
            WebElement element = driver.findElement(By.id(""));

            if (element.isEnabled()==true){
                
            }else {
                Assert.fail("未找到");
            }
        }
        
    }

ExpectedCondition

等待元素直到可点击状态
WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

隐式等待

查找WebDriver无法使用的元素时等待,默认0,生命周期整个WebDriver,跑出NoSuchElementException异常

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Assert.fail("没有找到元素”);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值