Automated test--selenium+python Determine if elements are visible and set timeout time-outs when elements do not appear

Source: Internet
Author: User
Tags assert

In automated testing, it is sometimes possible to assert whether an element is visible or not, and to determine whether an element is visible, as follows:

 from Import Expected_conditions as EC def is_element_visible (self, Element):     = self.driver    try:        = ec.visibility_of_element_located (Element)         assert  the_element (driver)        = True    except:        = False    return flag

This method is called when a judgment is required.

Sometimes after the operation, an element needs a period of time before the display, you can set a time limit, in this time interval to constantly determine whether the element is visible, if found to continue subsequent operations, otherwise the hint element is not found. The code is as follows:

 fromSelenium.webdriver.common.byImport by fromDatetimeImportdatetimethe_element= Is_element_visible (self, (by.id,"kw"))ifthe_element:Print "element appears."Else: Time_start=DateTime.Now () while  notthe_element:recheck_the_element= Is_element_visible (self, (by.id,"kw")) Time_now=DateTime.Now ()ifrecheck_the_element:Print "element appears."             Break        #The timeout limit here is set to 10 seconds        elif(Time_now-time_start). seconds > 10:            Print "element is invisible!"             Break        Else:            Continue

Automated test--selenium+python Determine if elements are visible and set timeout time-outs when elements do not appear

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.