Selenium兼容性問題注意

不知道是selenium兼容做的不好,還是瀏覽器自身運行速度和解析的關係,每次項目在chrome上跑得時候沒有問題,可以到chrome和safari上就有很多問題出現。下面一一總結出現的問題以及解決方案。


1、當頁面跳轉後,防止jquery ajax等事件未加載出需要操作的元素時,可以使用下面代碼控制元素出現後再執行操作。

public static void waitForPage(WebDriverWait wait, By by) {
        wait.until(ExpectedConditions.presenceOfElementLocated(by));
    }


2、當頁面在chrome上跳轉時,驗證跳轉的url是否和excepted的url一致時,會出現運行過快url判斷還停留在上一個url的問題。

/**
     * set i=50 (one is 100 millseconds) represent 5 seconds
     * @param url expected url
     */
    public static void pageload(String url){
        WebDriver driver = new IndexPage().getDriver();
        int i=0;
        while(i < 50) {
            i++;
            if(driver.getCurrentUrl().equals(url)) {
                break;
            } else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


3、當在chrome上運行彈框時,會出現如下錯誤:NoSuchAlertError: no alert open,解決方案可能有的人在Alert之前sleep一秒,但是我覺得這樣做是不正確的,正確的做法如下:

public static void waitForAlert(WebDriverWait wait) {
        wait.until(ExpectedConditions.alertIsPresent());
    }


發佈了160 篇原創文章 · 獲贊 21 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章