日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中wait的场景,wait——webdriver实用指南java版

發布時間:2024/8/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中wait的场景,wait——webdriver实用指南java版 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

Wait類的使用場景是在頁面上進行某些操作,然后頁面上就會出現或隱藏一些元素,此時使用WebDriverWait類的until方法來等待這些效果完成以便進行后續的操作。另外頁面加載時有可能會執行一些ajax,這時候也需要去WebDriverWait的until的等待ajax的請求執行完畢。

具體一點的例子前面也曾出現過,點擊一個鏈接然后會出現一個下拉菜單,此時需要先等待下拉菜單出現方可進行點擊菜單項的操作。

在實例化WebDriverWait類時,有下面2個構造方法

public WebDriverWait(WebDriver driver, long

timeOutInSeconds)

public WebDriverWait(WebDriver driver, long timeOutInSeconds,

long sleepInMillis)

其參數為

WebDriver driver。不言而喻

long timeOutInSeconds。總體的超時時間,最多等這么久。

long sleepInMillis。每隔多久去檢查一次until的結果

另外要注意的是,默認情況下,unitl中的NotFoundException會被忽略,但是其他異常還是正常傳播的。你可以通過ignoring(exceptions

to add)自己定義需要忽略的異常。

代碼

下面代碼演示了點擊按鈕后如何等待label出現。這個例子其實沒有前面的下拉菜單例子實用。

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

// ?點擊按鈕

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();

}

}

總結

以上是生活随笔為你收集整理的java中wait的场景,wait——webdriver实用指南java版的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。