国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

java selenium智能等待頁面加載完成示例代碼

2024-07-10 13:31:16
字體:
來源:轉載
供稿:網友

java selenium  智能等待頁面加載完成

我們經常會碰到用selenium操作頁面上某個元素的時候, 需要等待頁面加載完成后, 才能操作。  否則頁面上的元素不存在,會拋出異常。 

或者碰到AJAX異步加載,我們需要等待元素加載完成后, 才能操作

selenium 中提供了非常簡單,智能的方法,來判斷元素是否存在. 

閱讀目錄

  1. 實例要求
  2. 隱式等待
  3. 顯式等待

實例要求

實例:set_timeout.html 下面的html 代碼,  點擊click 按鈕5秒后, 頁面上會出現一個紅色的div快, 我們需要寫一段自動化腳本智能的去判斷這個div是否存在, 然后把這個div 然后高亮。

<html> <head>  <title>Set Timeout</title>  <style>   .red_box {background-color: red; width = 20%; height: 100px; border: none;}  </style>  <script>   function show_div(){    setTimeout("create_div()", 5000);   }    function create_div(){    d = document.createElement('div');    d.className = "red_box";    document.body.appendChild(d);   }  </script> </head> <body>  <button id = "b" onclick = "show_div()">click</button> </body></html>

隱式等待

  WebDriver driver = new FirefoxDriver();  driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html");     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);  WebElement element = driver.findElement(By.cssSelector(".red_box"));    ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = /"5px solid yellow/"",element); 

其中

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

意思是, 總共等待10秒, 如果10秒后,元素還不存在,就會拋出異常  org.openqa.selenium.NoSuchElementException

顯式等待

顯式等待 使用ExpectedConditions類中自帶方法, 可以進行顯試等待的判斷。

顯式等待可以自定義等待的條件,用于更加復雜的頁面等待條件

 

等待的條件

WebDriver方法

頁面元素是否在頁面上可用和可被單擊

elementToBeClickable(By locator)

頁面元素處于被選中狀態

elementToBeSelected(WebElement element)

頁面元素在頁面中存在

presenceOfElementLocated(By locator)

在頁面元素中是否包含特定的文本

textToBePresentInElement(By locator)

頁面元素值

textToBePresentInElementValue(By locator, java.lang.String text)

標題 (title)

titleContains(java.lang.String title)

 

只有滿足顯式等待的條件滿足,測試代碼才會繼續向后執行后續的測試邏輯

如果超過設定的最大顯式等待時間閾值, 這測試程序會拋出異常。

public static void testWait2(WebDriver driver) {  driver.get("E://StashFolder//huoli_28@hotmail.com//Stash//Tank-MoneyProject//浦東軟件園培訓中心//我的教材//Selenium Webdriver//set_timeout.html");     WebDriverWait wait = new WebDriverWait(driver, 20);  wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));  WebElement element = driver.findElement(By.cssSelector(".red_box"));    ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = /"5px solid yellow/"",element);  }

以上就是對Java selenium 等待頁面加載的資料整理,后續繼續補充相關資料,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴川市| 瑞安市| 上思县| 个旧市| 宜兰县| 合山市| 黄石市| 汽车| 霸州市| 太原市| 汤原县| 当阳市| 呼和浩特市| 平度市| 新建县| 新河县| 德阳市| 高安市| 祁东县| 林甸县| 普安县| 周宁县| 张家界市| 岳阳市| 互助| 甘洛县| 酉阳| 青田县| 玉林市| 遂平县| 成武县| 古丈县| 宜黄县| 保德县| 江都市| 广河县| 郓城县| 恩施市| 威信县| 建昌县| 饶河县|