WebDriver- 等待頁面加載元素完成

隱性等待是指當要查找元素,而這個元素沒有馬上出現時,告訴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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章