聊聊selenium的webdriver的超時參數

本文主要介紹下selenium的webdriver的超時參數。html

超時參數

selenium-api-2.53.1-sources.jar!/org/openqa/selenium/WebDriver.javajava

/**
   * An interface for managing timeout behavior for WebDriver instances.
   */
  interface Timeouts {

    /**
     * Specifies the amount of time the driver should wait when searching for an element if it is
     * not immediately present.
     * <p>
     * When searching for a single element, the driver should poll the page until the element has
     * been found, or this timeout expires before throwing a {@link NoSuchElementException}. When
     * searching for multiple elements, the driver should poll the page until at least one element
     * has been found or this timeout has expired.
     * <p>
     * Increasing the implicit wait timeout should be used judiciously as it will have an adverse
     * effect on test run time, especially when used with slower location strategies like XPath.
     *
     * @param time The amount of time to wait.
     * @param unit The unit of measure for {@code time}.
     * @return A self reference.
     */
    Timeouts implicitlyWait(long time, TimeUnit unit);

    /**
     * Sets the amount of time to wait for an asynchronous script to finish execution before
     * throwing an error. If the timeout is negative, then the script will be allowed to run
     * indefinitely.
     *
     * @param time The timeout value.
     * @param unit The unit of time.
     * @return A self reference.
     * @see JavascriptExecutor#executeAsyncScript(String, Object...)
     */
    Timeouts setScriptTimeout(long time, TimeUnit unit);

    /**
     * Sets the amount of time to wait for a page load to complete before throwing an error.
     * If the timeout is negative, page loads can be indefinite.
     *
     * @param time The timeout value.
     * @param unit The unit of time.
     * @return A Timeouts interface.
     */
    Timeouts pageLoadTimeout(long time, TimeUnit unit);
  }複製代碼

implicitlyWait

至關於設置全局的等待,在定位元素時,對全部元素設置超時時間,超出了設置時間則拋出異常。默認爲0即不等待。web

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
沒有設置implicitlyWait,則driver.findElement()只會找一遍指定元素,找不到就立刻拋異常api

scriptTimeout

設置異步腳本執行的超時時間,超出則拋異常。bash

The amount of time, in milliseconds, this session should wait for asynchronous scripts to finish executing. If set to 0, then the timeout will not fire until the next event loop after the script is executed. This will give scripts that employ a 0-based setTimeout to finish.session

pageLoadTimeout

用來設置頁面徹底加載的超時時間,徹底加載即頁面所有渲染。超過這個時間則拋出異常。默認爲-1,即永不超時。異步

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.jsp

這裏是否包含異步腳本都執行完成呢,通常onload是指外部資源加載完,而異步腳本執行完跟這個應該不是一回事。async

doc

相關文章
相關標籤/搜索