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