UI Automation wait several commonly used method

  For various reasons (business logic reason, the network itself, the reason server response, etc.), you need to operate code to wait! Currently waiting in python UI Automation most commonly used are several, for your reference analysis:

First, the universal time class

Import time class [Import time] using the time.sleep () from a sleep time or introduced in, [from time import sleep], using sleep (), can be written after each step of the code, this method waits for flexibility and freedom, but there is a downside to write back time is completed must be performed before they can execute the code behind, relatively time-consuming! There is uncertainty due to lack of time, then we will wait for the code error! For example, the code is written, sleep (5), wait / sleep five seconds, but the network delay may take 10 seconds to refresh a page or an element to be queried, so it will not be found because the relevant elements of the code error causes the program to break! There is the added sleep (5) seconds, but after one second refresh program, and the remaining 4 seconds wasted!

Two, driver.implicitly_wait () hidden waiting / waiting intelligent 

This method applies only to webdriver service, using the UI Automation, not in interface automation or other python code! After use, driver.implicitly_wait (10) to execute the code at all of the code applies! Its idea is to perform before the next element in the positioning of each element is positioned less than if there will be waiting, every (0.5 seconds) requests about the elements, (estimated to be refreshed once every 0.5 seconds, this is not important!) To set up 10 seconds after the request if the element has not been given before! On the line is the value you entered! This greatly shortens the waiting time, and unlike sleep () method as many times to write the code! This in a conversation just write it once! Greatly reducing the amount of code!

Three, WebDriverWait (Driver , timeout , poll_frequency = 0.5 , ignored_exceptions = None ) to wait for explicit

First import the package: from selenium.webdriver.support.wait import WebDriverWait

Some elements need some time to appear and operate (in some business logic used to control, as the button is grayed out only after certain conditions are met can click the like, this condition may require some delay to achieve) Similarly, the waiting the method also can be used only in webdriver. Specific examples of the method using:

Before the open browser, open the page is ignored, first put a hidden element to customize it: ls = '//*[text()="xxx"]/../a' (multi-level elements positioned. The representative of the parent) 

                 Next, examples of a waiting objects ll = WebDriverWait (driver, 30,3) .until (lambda driver: driver.find_element_by_xpath (ls)) ---> 30 is the upper limit of 3 is 3 seconds at every refresh request, further behind can be followed by arguments, it is beyond anything unusual after report

                 ---> lambda is an anonymous function

                 Finally, the element is present, the operating element ll.click ()

These are the three kinds of methods to wait for finishing, just read someone else's blog lists did not elaborate, I believe that each white see my blog can give you help! Thank you for your support! Comments welcome!

                  

Guess you like

Origin www.cnblogs.com/shaobai/p/11494002.html