WebDriver- 等待頁面載入元素完成
阿新 • • 發佈:2019-01-05
隱性等待是指當要查詢元素,而這個元素沒有馬上出現時,告訴WebDriver查詢Dom一定時間。預設值是0,但是設定之後,這個時間將在WebDriver物件例項整個生命週期都起作用
<span style="font-size:14px;">package com.test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Test_waitfor2 { public static void main(String[] args) { String url = "file:///C:/Documents and Settings/fei yong/桌面/wait.html"; //開啟chrome WebDriver dr = new ChromeDriver(); dr.get(url); //設定10秒 dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); dr.findElement(By.id("b")).click(); WebElement element = dr.findElement(By.cssSelector(".red_box")); System.out.println("獲取.red_box的背景顏色屬性值:"+element.getCssValue("background-color")); //在紅色區域外面加黃框 ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element); //dr.quit(); } } </span>