0 votes
1 view
in Devops and Agile by (24.8k points)

Trying to find a good way to set a maximum time limit for command execution latency in Selenium Python WebDriver. Ideally, something like:

my_driver = get_my_driver()

my_driver.set_timeout(30) # seconds

my_driver.get('http://www.example.com') # stops / throws exception when time is over 30     seconds

would work. I have found .implicitly_wait(30), but I'm not sure if it results in the desired behavior.

In case it is useful, we are specifically using the WebDriver for Firefox.

EDIT

 This might be useful:

ff = webdriver.Firefox()

ff.implicitly_wait(10) # seconds

ff.get("http://somedomain/url_that_delays_loading")

myDynamicElement = ff.find_element_by_id("myDynamicElement")

However, it is not clear to me whether the implicit wait applies both to get (which is the desired functionality) and to find_element_by_id.

1 Answer

0 votes
by (63k points)

The default WebDriver setting for timeouts is never.  WebDriver will sit there forever waiting for the page to load.

 The following timeouts are available:

/**

   * 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

     */

    Timeouts pageLoadTimeout(long time, TimeUnit unit);

  }

    

You can tweak the timeouts by setting driver.manage().timeouts().pageLoadTimeout()

 

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...