selenium 等待頁面加載完成


一、隱形加載等待:

file:///C:/Users/leixiaoj/Desktop/test.html 該頁面負責創建一個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>
等候10s后再修改div
//隱式等待
public
static void main(String[] args) throws InterruptedException { WebDriver dr = new FirefoxDriver(); //設置10 秒 dr.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); String url = "file:///C:/Users/leixiaojiang/Desktop/test.html"; dr.get(url); dr.findElement(By.id("b")).click(); WebElement element =dr.findElement(By.cssSelector(".red_box")); ((JavascriptExecutor)dr).executeScript("arguments[0].style.border =\"5px solid yellow\"",element); }
       //顯式等待
       WebDriver dr = new FirefoxDriver();
        
        String url = "file:///C:/Users/leixiaojiang/Desktop/test.html";
        dr.get(url);
        WebDriverWait wait = new WebDriverWait(dr,10);
        wait.until(new ExpectedCondition<WebElement>(){
        @Override
        public WebElement apply(WebDriver d) {return d.findElement(By.id("b"));}
        }).click();
        //上面的點擊操作5s后才有red_box這個類出現
        WebDriverWait wait1 = new WebDriverWait(dr,10);
        WebElement element =dr.findElement(By.cssSelector(".red_box"));
        ((JavascriptExecutor)dr).executeScript("arguments[0].style.border =\"5px solid yellow\"",element);

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM