Selenium three kinds of elements that appear in the standby mode

Selenium wait way of the following three elements appear
1, explicitly wait for
popular point that is dead, and so on, very rigid and inflexible wait.
That is, within the specified time must wait until an element appear or operational state, if not wait, we have been waiting, until the elements within the prescribed period of operation have still not found, then throws Exception

  WebDriverWait wait = new WebDriverWait(driver, 20)
  /**
     * 等待页面元素出现
     *
     * @param xpath
     */
    public void waitWebElementDisplay(String xpath) {
        long startTime = System.currentTimeMillis();
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
        long endTime = System.currentTimeMillis();
        float seconds = (endTime - startTime) / 1000F;

        log.debug("等待时间:" + Float.toString(seconds) + " seconds.");
    }

   /**
     * 等待输入框enable
     *
     * @param webElement
     */

    public void waitElementClickable(WebElement webElement) {
        long startTime = System.currentTimeMillis();
        wait.until(ExpectedConditions.elementToBeClickable(webElement));
        long endTime = System.currentTimeMillis();
        float seconds = (endTime - startTime) / 1000F;

        log.debug("等待时间:" + Float.toString(seconds) + " seconds.");
    }

The code, set the time to wait is 20 seconds. If you do not meet until () method of the conditions, it will always be here to wait 20 seconds, and still can not find it throws an exception.

2, implicit wait
implicit wait statement in the entire script automated tests do not see an implicit wait. But it will automatically wait each time the page is loaded; implicit waiting just declare once, usually declared after opening the browser (that is, after the browser loads the corresponding driver). After declaring the entire life cycle of the instances of WebDriver are valid, no need to repeat the follow-up statement. If you do not set the global element of the timeout, the default timeout is 0.
. driver.manage () timeouts () implicitlyWait (1, TimeUnit.SECONDS); // 1 refers to wait one second.
For more details, please refer to the article: Implicit wait

3, forced to wait for
that is the thread to sleep, forced thread currently executing code hibernation (suspend)
Thread.sleep (Long of millis);

Note that, the three waiting mode, if the display and waiting waiting implicit use, the maximum waiting time to the maximum waiting time two.

Reproduced in: https: //www.jianshu.com/p/b06a8b9bee24

Guess you like

Origin blog.csdn.net/weixin_34010949/article/details/91099009