java中wait的场景,wait——webdriver实用指南java版
場(chǎng)景
Wait類的使用場(chǎng)景是在頁面上進(jìn)行某些操作,然后頁面上就會(huì)出現(xiàn)或隱藏一些元素,此時(shí)使用WebDriverWait類的until方法來等待這些效果完成以便進(jìn)行后續(xù)的操作。另外頁面加載時(shí)有可能會(huì)執(zhí)行一些ajax,這時(shí)候也需要去WebDriverWait的until的等待ajax的請(qǐng)求執(zhí)行完畢。
具體一點(diǎn)的例子前面也曾出現(xiàn)過,點(diǎn)擊一個(gè)鏈接然后會(huì)出現(xiàn)一個(gè)下拉菜單,此時(shí)需要先等待下拉菜單出現(xiàn)方可進(jìn)行點(diǎn)擊菜單項(xiàng)的操作。
在實(shí)例化WebDriverWait類時(shí),有下面2個(gè)構(gòu)造方法
public WebDriverWait(WebDriver driver, long
timeOutInSeconds)
public WebDriverWait(WebDriver driver, long timeOutInSeconds,
long sleepInMillis)
其參數(shù)為
WebDriver driver。不言而喻
long timeOutInSeconds??傮w的超時(shí)時(shí)間,最多等這么久。
long sleepInMillis。每隔多久去檢查一次until的結(jié)果
另外要注意的是,默認(rèn)情況下,unitl中的NotFoundException會(huì)被忽略,但是其他異常還是正常傳播的。你可以通過ignoring(exceptions
to add)自己定義需要忽略的異常。
代碼
下面代碼演示了點(diǎn)擊按鈕后如何等待label出現(xiàn)。這個(gè)例子其實(shí)沒有前面的下拉菜單例子實(shí)用。
wait.html
< html>
< head>
<
meta http-equiv="content-type" content="text/html;charset=utf-8"
/>
<
title>wait
<
script type="text/javascript" async=""
src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
<
link
href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet" />
<
script type="text/javascript">
$(document).ready(function(){
$('#btn').click(function(){
$('
waitr-webdriver
').css('margin-top', '1em').insertAfter($(this));
$(this).addClass('disabled').unbind('click');
});
});
<
/script>
< /head>
< body>
<
div class="row-fluid">
< div class="span6 well">
< h3>wait
< button class="btn
btn-primary" id="btn" >Click
< /div>
<
/div>
< /body>
< script
src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js">
< /html>
wait.java
import
java.io.File;
import
java.util.List;
import
org.openqa.selenium.Alert;
import
org.openqa.selenium.By;
import
org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.Keys;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.openqa.selenium.support.ui.ExpectedCondition;
import
org.openqa.selenium.support.ui.WebDriverWait;
public class WaitExample
{
public static void main(String[] args) throws
InterruptedException {
WebDriver
dr = new ChromeDriver();
File file
= new File("src/wait.html");
String
filePath = "file:///" + file.getAbsolutePath();
System.out.printf("now accesss %s \n", filePath);
dr.get(filePath);
Thread.sleep(1000);
// ?點(diǎn)擊按鈕
dr.findElement(By.id("btn")).click();
(new
WebDriverWait(dr, 10)).until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return
d.findElement(By.className("label")).isDisplayed();
}
});
Thread.sleep(1000);
System.out.println("browser will be close");
dr.quit();
}
}
總結(jié)
以上是生活随笔為你收集整理的java中wait的场景,wait——webdriver实用指南java版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NS2相关学习——完成一个新的协议(1)
- 下一篇: mysql 二维数组下标_php二维数组