In the process of UI automation, we sometimes need to add some wait time to help us wait for elements to appear, but sometimes too much or too little time is added, which is impossible to judge. Today we introduce several wait times. Let's see which one is right for us, and we'll use which one
Forced Wait
You should know when you see the name. Forced wait is how many seconds you set and how many seconds you have to wait to continue
time.sleep()
def sleep(seconds): # real signature unknown; restored from __doc__ """ sleep(seconds) Delay specified number of seconds """ pass
Usage method
#Add directly where you need to wait
time.sleep(10)
Implicit Wait
Implicit wait: implicitly_wait?() The default parameter is in nice units, so setting a wait time does not affect the execution speed of the script.When a script finds an element positioned, it proceeds if the element can be positioned, and if the element cannot be positioned, it will continuously poll to determine if the element is positioned.Assume that the execution continues if the element is located in the sixth second, and throws an exception if the element has not been located for 10 seconds beyond the set time.
def implicitly_wait(self, time_to_wait): """ Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session. To set the timeout for calls to execute_async_script, see set_script_timeout. :Args: - time_to_wait: Amount of time to wait (in seconds) :Usage: driver.implicitly_wait(30) """ if self.w3c: self.execute(Command.SET_TIMEOUTS, { 'implicit': int(float(time_to_wait) * 1000)}) else: self.execute(Command.IMPLICIT_WAIT, { 'ms': float(time_to_wait) * 1000})
Usage method:
# Add directly where you need to wait driver.implicitly_wait(10)
Activity Waiting
Activity Waiting: app has a unique kind of waiting to help us decide whether we have reached this page or not, and then do a series of actions by waiting_activity.
def wait_activity(self, activity, timeout, interval=1): """Wait for one activity,Until within the specified time activity Appear This is an Android-only method. :Args: - activity - target activity - timeout - max wait time, in seconds - interval - sleep interval between retries, in seconds """ try: WebDriverWait(self, timeout, interval).until( lambda d: d.current_activity == activity) return True except TimeoutException: return False
Usage method:
Add directly where you need to wait for elements to appear
# Add to activity,Add wait time after, error if time exceeds driver.wait_activity('com.ali.user.mobile.login.ui.UserLoginActivity',30)
Show Waiting
Displays the wait that was meant to be introduced when writing the selenium tutorial, but feels like it will be used later, so I'll introduce it directly here.
First look at the source code:
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None): """ driver: Return a webdriver instantiation timeout: Set a time-out period ( S) poll_frequency: Cyclic time to read elements, default is 0.5(s) //Usage method: from selenium.webdriver.support.ui import WebDriverWait \n element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n until_not(lambda x: x.find_element_by_id("someId").is_displayed()) """ self._driver = driver self._timeout = timeout self._poll = poll_frequency # avoid the divide by zero if self._poll == 0: self._poll = POLL_FREQUENCY exceptions = list(IGNORED_EXCEPTIONS) if ignored_exceptions is not None: try: exceptions.extend(iter(ignored_exceptions)) except TypeError: # ignored_exceptions is not iterable exceptions.append(ignored_exceptions) self._ignored_exceptions = tuple(exceptions)
Role of three parameters allotted from source code
driver: returns a webdriver instantiation
Timeout: Set a timeout duration
poll_frequentcy: Loop read element time
Usage method:
# Import Method from selenium.webdriver.support.ui import WebDriverWait element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))
Where until indicates what needs to be executed
Note: The difference between Show Wait and Implicit Wait is one to wait for elements to load and the other to wait for pages to load
There are actually many ways to wait. Quiet here is only a simple introduction of the three, which one to use, which to use, thank you for your reading. If the writing is helpful, you can pay attention to the bottom right corner.