Selenium兼容性问题注意

来源:互联网 发布:js创建json数组对象 编辑:程序博客网 时间:2024/06/16 06:20

不知道是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());    }


0 0
原创粉丝点击