栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

元素三大等待

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

元素三大等待

目录

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");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782769.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号