首页 技术 正文
技术 2022年11月20日
0 收藏 889 点赞 5,158 浏览 2707 个字

selenium webdriver学习—三种等待时间方法:显式等待,隐式等待,强制等待

本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作;

import java.util.Set;
import java.util.concurrent.TimeUnit;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;/**
* 方法3,隐式等待,隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
* 隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,
* 如果Element或数组没有马上被发现的话。默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。一劳永逸。
* ********
* 方法1,强制等待,固定休眠时间设置,Java的Thread类里提供了休眠方法sleep,导入包后就能使用,
* 执行到此时不管什么就固定的等待5秒之后再接着执行后面的操作
* ********
* 方法2,显式等待,在0-10s中去定位id为“su”的元素,WebDriverWait默认每500ms就调用一次ExpectedCondition
* 直到定位成功或者时间截止,ExpectedCondition的返回值要么为true要么为不为空的对象,
* 在规定时间内若没有定位成功元素,则until()会抛出org.openqa.selenium.TimeoutException 。*/public class YsfTest_20180726{
private static final int ExpectedCondition = 0;
private static final int Boolean = 0; public static void main(String[] args) throws InterruptedException{
WebElement search = null;
System.setProperty("webdriver.chrome.driver","C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
//方法3,隐式等待,implicitlyWait()方法比sleep()方法智能,sleep()方法只能在一个固定的时间等待,而implicitlyWait()可以在一个时间范围内等待,称为隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.baidu.com/");
//窗口最大化
driver.manage().window().maximize();
//定位所搜框,并输入电影
driver.findElement(By.cssSelector("#kw")).sendKeys("电影");
//方法1,强制等待,固定休眠时间设置,sleep()方法以毫秒为单位 ,此类为设置5s等待
Thread.sleep(5000);
//定位百度一下按钮
WebElement searchButton = driver.findElement(By.cssSelector("#su"));
//点击百度一下
searchButton.submit();
//获得当前窗口url
String parentUrl = driver.getCurrentUrl();
System.out.println("currenturl:"+parentUrl);
//获取搜索navigate窗口
driver.navigate().to("https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=navigate");
//方法2,显式等待
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver d){
return d.getTitle().toLowerCase().startsWith("navigate");
}
});
//获得当前窗口url
String parentUrl2 = driver.getCurrentUrl();
System.out.println("currenturl:"+parentUrl2);
//刷新页面
driver.navigate().refresh();
//后退到百度首页
driver.navigate().back();
//方法2,显式等待,在规定时间内等待 在10秒的范围内 出现.red_box元素就往下执行,如果10秒过后还没出现就跳出
WebElement element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
//前进到搜索navigate窗口
driver.navigate().forward();
driver.close();
} }

注意,如果显式等待搜索的内容不存在,则会跑出异常;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,947
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,473
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,286
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,102
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,734
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,769