Python + Selenium notes (10): Element Wait Mechanism, pythonselenium
(1)Preface
If the target element cannot be found due to sudden resource limitations or network latency, the test report shows that the test fails. In this case, a latency mechanism is required to match the running speed of the script with the response speed of the program. In this case, WebDriver provides two mechanisms: Implicit wait and explicit wait.
(2)Implicit wait
Once the implicit wait time is set, it will apply to the entire life cycle of the WebDriver instance (effective for all element searches). After the implicit wait time is set, webdriver continuously detects and searches for DOM within a certain period of time to find one or more elements that are not successfully loaded and available immediately. The default time for implicit waiting is 0. WebDriver uses implicitly_wait () to set the waiting time, in seconds. The NoSuchElementException exception is reported if the wait time is not found.
#Set the timeout value to 10 seconds.
Driver. implicitly_wait (10)
(3)Explicit waiting
The WebDriver provides the WebDriverWait class and expected_conditions modules for explicit waiting. Compared with implicit wait, the display wait is more intelligent. Display wait is to set a precondition. During the waiting time, check whether the precondition is met at intervals. If the precondition is met, execute the next step. If the timeout occurs, a TimeoutException exception is reported.
(4)WebDriverWaitClass
WebDriverWait(driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None)
Driver: browser driver instance
Timeout: waiting time, in seconds
Poll_frequency: the time interval for checking. The default value is 0.5 seconds.
Ignored_exceptions: Ignore exception. The default value is NoSuchElementException.
Until method and until_not method:
Until (method, message =''): Method refers to the method to be executed (execute once every other period of time within the waiting time) until the return value is true. If the timeout value is exceeded, A TimeoutException exception is reported, message will be passed in an exception (message Parameter is optional)
Until_not (method, message =''): Until the returned value is false, the others are the same as the.
(5)Expected_conditionsModule
The expected_conditions module provides multiple predefined preconditions that must be used with WebDriverWait.
|
Expected waiting condition (Preconditions) |
Simple Description |
element_to_be_clickable(locator)
Parameter: locator, which refers to a group (By, locator)
Example: WebDriverWait (driver, 10). until (expected_conditions.element_to_be_clickable (By. NAME,'11')))
The following are all in this way, but the preconditions are different, and the transmitted parameters may be different.
WebDriverWait(driver,10).until() |
The element to be searched is visible and available, so that you can click to return the located element.
|
element_to_be_selected(locator) |
Wait until the element is selected |
invisibility_of_element_located(locator)
|
Wait for an element to be invisible or nonexistent in the DOM
|
presence_of_all_elements_located(locator)
|
Wait until at least one of the locator's search elements appears on the webpage and return a group of elements
|
presence_of_element_located(locator)
|
Wait for the element found by the locator to appear on the webpage, or you can find it in the DOM and return a located element.
|
text_to_be_present_in_element(locator,text)
Parameter: text, the specified text |
Wait for the element to be located and contain the specified text information
|
title_contains(title)
Parameter: title, which indicates the string containing the title to be verified. |
Returns True if the webpage title contains the specified string. Otherwise, returns false.
|
title_is(title)
Parameter: title, the title to be verified |
If the webpage title is the same as expected, True is returned when the webpage title is successful. Otherwise, false is returned.
|
visibility_of(element)
Parameter: element, which refers to an element.
|
Wait for the element to appear in the DOM. it is visible and the width and height are greater than 0. If it becomes visible, an element (the same) is returned)
|
visibility_of_element_located(locator)
|
Wait for the element to appear in the DOM. it is visible and the width and height are greater than 0. If it becomes visible, an element is returned.
|
alert_is_present()
|
Determine whether a warning window exists |
(6)Expected_conditionsExample
The following code, try: in part, each part is independent and available (I just commented out after verifying the usage of different preconditions ). In addition, we only describe how to use the method (the function of the method) Here, And do not describe the use scenario (for example, whether it is necessary to do so or not ).
1 from selenium import webdriver 2 from selenium. webdriver. support. ui import WebDriverWait 3 from selenium. webdriver. support import expected_conditions 4 from selenium. webdriver. common. by import By 5 6 driver = webdriver. firefox () 7 driver. maximize_window () 8 driver. get ('https: // www.cnblogs.com/') 9 10 # try: 11 # Wait for the [find and view] button on the blog homepage to be visible and available 12 # search_btn = WebDriverWait (driver, 10 ). until (expected_conditions.element_to_be_clickable (. CLASS_NAME, 'search _ btn ') 13 # print (search_btn.get_attribute ('value') 14 15 # try: 16 # login_area = driver. find_element_by_css_selector ('# login_area') 17 # login = login_area.find_element_by_link_text ('login') 18 # login. click () 19 # remember_me = driver. find_element_by_id ('Remember _ me') 20 # remember_me.click () 21 # Wait until the check box on the logon page is selected 22 # WebDriverWait (driver, 10 ). until (expected_conditions.element_located_to_be_selected (. ID, 'Remember _ me ') 23 24 # try: 25 # search_file = driver. find_element_by_id ('zzk _ Q') 26 # search_btn = driver. find_element_by_class_name ('search _ btn ') 27 # search_file.send_keys ('python') 28 # search_btn.click () 29 # Whether the webpage title contains python30 # WebDriverWait (driver, 10 ). until (expected_conditions.title_contains ('python') 31 32 try: 33 search_file = driver. find_element_by_id ('zzk _ Q') 34 # Check whether the element is visible in the DOM, and the width and height are greater than 035 search_file = WebDriverWait (driver, 10 ). until (expected_conditions.visibility_of (search_file) 36 print (search_file) 37 finally: 38 driver. quit ()
(7)Example (custom preconditions)
The expected_conditions class provides a variety of pre-defined conditions (expected waiting conditions). When no pre-conditions are met, you can also customize the pre-conditions through WebDriverWait.
Below are some comments of the WebDriverWait class.
class WebDriverWait(object): def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None): """ Example: 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()) """
Example: (five options are available in the drop-down menu of the personal homepage (click the page to jump to the garden on the homepage)
# Lambda expressions are actually an anonymous function. The left side of the colon can be understood as the function name and parameter, and the right side can be understood as the return value of the function. For details, refer to Baidu python lambda.
From selenium import webdriverfrom selenium. webdriver. support. ui import WebDriverWaitfrom selenium. webdriver. support. ui import Selectprofile = webdriver. firefoxProfile \ (r 'C: \ Users \ quanhua \ AppData \ Roaming \ Mozilla \ Firefox \ Profiles \ tnwjkr4m. selenium ') driver = webdriver. firefox (profile) driver. maximize_window () driver. get ('https: // home.cnblogs.com/') try: # Wait for five optional WebDriverWait (driver, 10) drop-down menus on the personal homepage of the blog garden ). until (lambda l: len (Select (l. find_element_by_id ('sel _ application ')). options) = 5) finally: driver. quit ()
(8)Summary
The application element wait mechanism is essential for building highly stable and reliable tests. During use, try to avoid implicit waiting and display waiting for mixed use. As for the advantages and disadvantages of implicit waiting and display waiting, it is generally recommended to use display waiting for reading and online, but I tried it myself, for the time being, I didn't see how different the running speed is. (I may wait for a lot of project experience to come back and talk about the advantages and disadvantages of implicit waiting and display waiting ).