webdriver的2种等待

隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间,默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

显式等待:selenium webdriver提供了一些方法帮助我们等待正好需要等待的时间。利用WebDriverWait类和ExpectedCondition接口就能实现这一点。

        WebDriverWait wait = new WebDriverWait(dr,10);
        wait.until(new ExpectedCondition<WebElement>(){
        @Override
        public WebElement apply(WebDriver d) {
            return d.findElement(By.className("red_box"));
        }

 

        WebDriverWait wait = new WebDriverWait(dr,10);
        // 创建一个新的ExpectedCondition接口,实现apply方法
        boolean flag = wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {

                return d.findElement(by).isDisplayed();
            }
        });

Java线程等待(Thread.sleep()),这种方最不好,因为一个元素的加载时间有长有短,你在设置sleep的时间之前要自己把握长短,太短容易超时,太长浪费时间。

 

posted @ 2017-08-10 13:12  wujuan  阅读(265)  评论(0编辑  收藏  举报