Webdriver中寻找元素超时(pageLoadTimeout 、mplicitlyWait和WebDriverWait适用情况)和在指定元素上执行js方法

1.pageLoadTimeout 设置页面加载超时时间:

//设定在加载页面时间为15秒,如果在加载时间还不能加载完页面那会抛出超时异常我们可以观察我们需要的元素加载的时间,之后就可以找到并处理了

driver.manage().timeouts().pageLoadTimeout(15,TimeUnit.SECONDS);

在15秒之后如果页面还没有加载完成则抛出超时异常;这时我们应该对上面进行异常捕获。

2. implicitlyWait 设置寻找一个元素的时间

driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);

//寻找一个7秒内一个元素,如果7秒内没有找到,则抛出NoSuchElement异常

3. new WebDriverWait(driver,3,1000);

//每1秒检查一次,最多等待3s (默认0.5s) 找不到元素会抛出NoSuchElement异常,如果找到了那就马上返回。

问题:1,2都可以设置等待元素的超时时间,那具体是按照哪个的时间作为超时时间呢?

a. 当implicitlyWait 的和WebDriverWait同时设置了超时时:

我们试着找应该没有的元素:

//		webDriverWait = new WebDriverWait(driver,20,1000);
//		driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
		WebElement wInfoNo = null;
		nowTime = System.currentTimeMillis();
		try {
			//driver.manage().timeouts().implicitlyWait会使用设置的时间进行超时
			wInfoNo = driver.findElement(By.className("ln11"));
//			wInfoNo	= webDriverWait.until(new ExpectedCondition<WebElement>() {
//				@Override
//				public WebElement apply(WebDriver input) {
//					return driver.findElement(By.className("ln11"));
//				}
//			});
		} catch (Exception e) {
			System.out.println("找不到");
			lastTime = System.currentTimeMillis();
		}
		if(lastTime==0){
			System.out.println("找到了");
			System.out.println(""+(System.currentTimeMillis()-nowTime));
		}else{
			System.out.println(""+(lastTime-nowTime));
		}
结果: 我们没有设置implicitlyWait,则driver.findElement()只会找一遍指定元素,找不到就马上抛异常


b.当我们设置implicitlyWait时:(打开设置implicitlyWait的代码) 结果:


c.加上设置WebDriverWait webDriverWait = new WebDriverWait(driver,20,1000);


总结:我们可以发现driver.findElement()使用的是implicitlyWait的超时时间

d.使用WebDriverWait对象寻找元素会怎么样呢?

		WebDriverWait webDriverWait = new WebDriverWait(driver,3,1000);
		driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
		WebElement wInfoNo = null;
		nowTime = System.currentTimeMillis();
		try {
//			wInfoNo = driver.findElement(By.className("ln11"));//会使用driver.manage().timeouts().implicitlyWait设置的时间进行超时
			wInfoNo	= webDriverWait.until(new ExpectedCondition<WebElement>() {
				@Override
				public WebElement apply(WebDriver input) {
					return driver.findElement(By.className("ln11"));
				}
			});
		} catch (Exception e) {
			System.out.println("找不到");
			lastTime = System.currentTimeMillis();
		}
		if(lastTime==0){
			System.out.println("找到了");
			System.out.println(""+(System.currentTimeMillis()-nowTime));
		}else{
			System.out.println(""+(lastTime-nowTime));
		}

结果好像webDriverWait 是使用的implicitlyWait的超时时间,但是我们把WebDriverWait 的超时时间的值设置比implicitlyWait大时会怎么样呢?

WebDriverWait webDriverWait = new WebDriverWait(driver,20,1000);


观察可以简单判断:

如果设置了1、webDriverWait的等待时间和2、implicitlyWait 的等待时间 如果1<=2则使用的是2的时间 如果1>2则使用1的时间但是要比1的时间长一些,具体长多少时间和webDriverWait、implicitlyWait设置的超时时间有关。


在java操作页面中的js:

JavascriptExecutorjse = (JavascriptExecutor)driver;
// arguments[0]一般指我们要操作的元素对象, arguments[1]指更新的值
StringsetStartDateJS = "arguments[0].setAttribute('realvalue', arguments[1]);";
//设置id为startDate的元素的realvalue的值为startDate
jse.executeScript(setStartDateJS,driver.findElement(By.id("startDate")),startDate);
String endtDate= getDate(queryModel,false);
//设置id为endDate的元素的realvalue的值为endDate
jse.executeScript(setStartDateJS,driver.findElement(By.id("endDate")),endtDate);







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值