元素三大等待

目录

1.硬性等待

2.隐式等待

 3.显式等待


1.硬性等待

让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作

  • 线程休眠,强制等待
  • Thread.sleep(long mills)
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ElementWait {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testWait() throws InterruptedException {
        openFirefox();
        firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
        firefoxDriver.findElement(By.id("su")).click();
        Thread.sleep(3000);
        firefoxDriver.findElement(By.xpath("/html/body/div[2]/div[4]/div[1]/div[3]/div[2]/div/div[1]/h3/a[1]/em")).click();
    }
    
    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

缺点:比如说有这样一个场景:元素1秒中就加载出来了,可是我们设置的等待时长为3秒,这时候就会浪费2秒的时间

2.隐式等待

在设置的超时时间范围内不断的查找元素,直到找到元素或者超时

设置方式:设置一次即可driver.manage.timeouts().implicitlyWait(long time,TimeUnit unit);

优点:相对灵活

缺点:设置是针对全局的,在webdriver实例整个生命周期有效,但并不是所有的元素都需要等待

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.concurrent.TimeUnit;

public class ElementWait {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testWait1(){
        openFirefox();
        //在driver实例化完成之后设置隐式等待,设置超时的时间为5秒
        //TimeUnit.SECONDS表示单位是秒
        firefoxDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

//...........
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}


 3.显式等待

用来等待某个条件发生后再继续执行后续代码(如找到元素、元素可点击、元素已显示等)

设置方式:

WebDriverWait wait = new WebDriverWait();

WebElement element = wait.until(expectCondition);

方法等待条件
visibilityOfElementLocated(By locator)页面元素早页面存在并且可见
elementToBeClickable(By locator)页面元素是否在页面上可用和可被单击
elementToBeSelected(WebElement element)页面元素处于被选中状态
textToBePresentInElement(By locator)在页面元素中是否包含特定的文本
presenceOfElementLocated(By locator)页面元素在页面中存在

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class ElementWait {
    private static FirefoxDriver firefoxDriver;

    @Test
    public void testWait3(){
        openFirefox();
        firefoxDriver.findElement(By.id("kw")).sendKeys("selenium");
        firefoxDriver.findElement(By.id("su")).click();
        //显式等待
        WebDriverWait webDriverWait = new WebDriverWait(firefoxDriver,5);
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[4]/div[1]/div[3]/div[2]/div/div[1]/h3/a[1]/em")));
        firefoxDriver.findElement(By.xpath("/html/body/div[2]/div[4]/div[1]/div[3]/div[2]/div/div[1]/h3/a[1]/em")).click();    
    }

    public static void openFirefox() {
        firefoxDriver = new FirefoxDriver();
        firefoxDriver.get("https://www.baidu.com");
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值