Selenium with Python 008-webdriver element waits

Source: Internet
Author: User
Tags set time

Most Web applications today use AJAX technology, and when a browser loads a page, the elements on the page may not be loaded at the same time, adding to the difficulty of positioning the element. If the elementnotvisibleexception occurs because of a delay in loading an element, the stability of the automation script is reduced, and we can set the element to wait to improve the instability caused by this problem.

Webdriver provides two types of wait: implicit wait and clear wait . Explicitly waiting for a specific block of code , so that webdriver wait for a condition to continue execution, or the maximum length of time to throw a time-out exception, and the implicit wait, the global timeout setting , Causes Webdriver to continually poll the DOM for the specified time to try to locate the element until the element is successfully positioned or timed out.

Clear Waiting

Way one: The more extreme way is to have the program hibernate for a specified time by Time.sleep (), and then continue execution.

Mode two: Combine webdriverwait and expectedcondition to set the wait. The Webdriverwait class is a wait method provided by the Webdriver, which detects the presence of the current page element at intervals over a set time. Specific formats such as: Webdriverwait (Driver,timeout,poll_frequency=0.5,ignored_exceptions=none)

#!/usr/bin/env python#-*-coding:utf-8-*- fromSeleniumImportWebdriver fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.support.uiImportwebdriverwait fromSelenium.webdriver.supportImportExpected_conditions as ECImportTimedriver=Webdriver. Chrome () Driver.get ("http://www.baidu.com") Wait= Webdriverwait (Driver, 5, 0.5) Element= Wait.until (ec.presence_of_element_located (By.id,"kw")) ) Element.send_keys ("Selenium") Time.sleep (5) Driver.close ()

As in the code block, after opening the Baidu home page, unless you can locate the id "KW" control to continue to execute, otherwise it will wait until 5s after the timeout exception is thrown. Webdriverwait default every 500 milliseconds to check expectedcondition

The Expected_conditions class provides the following methods for judging the expected conditions:

    • Title_is
    • Title_contains
    • presence_of_element_located
    • visibility_of_element_located
    • Visibility_of
    • presence_of_all_elements_located
    • Text_to_be_present_in_element
    • Text_to_be_present_in_element_value
    • Frame_to_be_available_and_switch_to_it
    • invisibility_of_element_located
    • Element_to_be_clickable
    • Staleness_of
    • element_to_be_selected
    • element_located_to_be_selected
    • Element_selection_state_to_be
    • Element_located_selection_state_to_be
    • Alert_is_present
Implicit waiting

An implicit wait tells Webdriver to poll the DOM for a certain amount of time when trying to find any element (or elements ) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the Webdriver object.

Implicitly_wait belongs to the global intelligent wait time, once set, acting on the entire webdriver life cycle, it is not a fixed wait time, once the element is positioned to continue execution.

#!/usr/bin/env python#-*-coding:utf-8-*- fromSeleniumImportWebdriverdriver=Webdriver. Chrome () driver.implicitly_wait (10) Driver.get ("http://www.baidu.com") Element= driver.find_element_by_id ("kw") Element.send_keys ("Selenium") Driver.close ()

Selenium with Python 008-webdriver element waits

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.