The use of Selenium’s three waiting methods!

UI automated testing mostly simulates actual production scenario operations by locating page elements. However, when writing automated test scripts, elements often cannot be located. The reasons are nothing more than two situations: 1. There is a frame; 2. There is no waiting setting.

This happens because the code running speed and the browser loading and rendering speed are not of the same order of magnitude. When doing WEB automation, you generally have to wait for the page elements to be loaded before performing the operation. Otherwise, an element not found error will be reported. This requires us to add waiting time in some scenarios: explicit waiting, implicit waiting, forced waiting. wait.

1. Display waiting

Definition: Wait for a certain condition to be met to continue execution, otherwise an exception (TimeoutException) will be thrown when the maximum duration is reached;

The WebDriverWait class is a waiting method provided by webdriver. When used together with the until() and until_not() methods provided by this class, you can wait flexibly according to the judgment conditions. The format is as follows:

1 WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
2 driver: browser driver
3 timeout: maximum timeout time
4 poll_frequency: detection interval, default 0.5s
5 ignored_exceptions: Exception information after timeout, NoSuchElementException exception is thrown by default
6 WebDriverWait() is generally used in conjunction with the until() or until_not method. The following is an explanation of these two methods:
7 until(method,message=''): Call the driver provided by this method as a parameter until the return value is True;
8 until_not(method,message=''): Call the driver provided by this method as a parameter until the return value is False;

The sample code is as follows:

 1 # coding = utf-8
 2 from selenium import webdriver
 3 from selenium.webdriver.support.wait import WebDriverWait
 4 from selenium.webdriver.support import expected_conditions as EC
 5 from selenium.webdriver.common.by import By
 6
 7 driver = webdriver.Chrome("F:\Installation Tools\python\chromedriver.exe")
 8 driver.implicitly_wait(10)
 9 driver.get('http://www.cnblogs.com/imyalost/')
10 locator = (By.LINK_TEXT, '老_张')
11
12 try:
13 WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
14 print(driver.find_element_by_link_text('老_张').get_attribute('href'))
15 finally:
16 driver.close()

Code analysis:

In this example, expected_conditions is renamed to EC through the as keyword, and the presence_of_element_located() method is called to determine whether the element exists;

In the above example, implicit waiting and explicit waiting are used at the same time, but it should be noted that the maximum waiting time is the maximum of the two;

The expected condition judgment methods provided by the expected_conditions class are as follows:

 1 title_is: Determine whether the title of the current page is completely equal to (==) the expected string and return a Boolean value
 2 title_contains: Determine whether the title of the current page contains the expected string and return a Boolean value
 3 presence_of_element_located: Determining whether an element has been added to the DOM tree does not mean that the element must be visible.
 4 visibility_of_element_located: Determine whether an element is visible. Visible means that the element is not hidden, and the width and height of the element are not equal to 0
 5 visibility_of: does the same thing as the above method, except that the above method needs to pass in the locator. This method can directly pass the located element.
 6 presence_of_all_elements_located: Determine whether at least 1 element exists in the dom tree. For example, if there are n elements on the page with classes all 'column-md-3', then as long as 1 element exists, this method will return True
 7 text_to_be_present_in_element: Determine whether the text in an element contains the expected string
 8 text_to_be_present_in_element_value: Determine whether the value attribute in an element contains the expected string
 9 frame_to_be_available_and_switch_to_it: Determine whether the frame can be switched in. If so, return True and switch in, otherwise return False
10 invisibility_of_element_located: Determine whether an element does not exist in the dom tree or is invisible
11 element_to_be_clickable: Determine whether an element is visible and enabled. In this case, it is called clickable.
12 staleness_of: Wait for an element to be removed from the DOM tree. Note that this method also returns True or False
13 element_to_be_selected: Determine whether an element is selected, generally used in drop-down lists
14 element_selection_state_to_be: Determine whether the selected state of an element meets expectations
15 element_located_selection_state_to_be: has the same effect as the above method, except that the above method passes in the located element, and this method passes in the locator
16 alert_is_present: Determine whether there is an alert on the page
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

2. Implicit waiting

Definition: Wait for the page element to be loaded for a set time, and then execute the following code. If the loading is not completed after the set time, continue to execute the following code (Note: If the loading is completed within the set time, execute the following code immediately. code);

The implicit waiting method is: implicitly_wait. The sample code is as follows:

1 # coding = utf-8
2 from selenium import webdriver
3
4 driver = webdriver.Chrome("F:\Installation Tools\python\chromedriver.exe")
5 driver.implicitly_wait(10) # Implicit wait, up to 10 seconds
6 driver.get('http://www.cnblogs.com/imyalost/')
7
8 print(driver.current_url)
9 driver.quit()

Code analysis:

In this example, the waiting time is set to 10 seconds, but this 10 seconds is not a fixed time and does not affect the script execution speed; secondly, the implicit waiting works for the entire driver cycle, so it only needs to be set once.

3. Forced waiting

That is, the sleep() method, provided by the time module in python, forces the code to wait for xxx time. Regardless of whether the previous code is completed or not yet completed, it must wait for the set time.

The sample code is as follows:

 1 # coding = utf-8
 2 from selenium import webdriver
 3 from time import sleep
 4
 5 driver = webdriver.Chrome("F:\Installation Tools\python\chromedriver.exe")
 6 driver.get('http://www.cnblogs.com/imyalost/')
 7
 8 sleep(5)
 9  
10 print(driver.current_url)
11 driver.quit()

Code analysis:

In this example, set the mandatory waiting time to 5 seconds. After 5 seconds, print the obtained URL of the current page, and then close the window. This method of forced waiting is very useful when debugging, but it is recommended to use this method with caution because it is too rigid and seriously affects the execution speed of the program!

For the above three waiting methods, in specific scenarios, you need to choose the appropriate method according to the situation and use it flexibly!

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview documents

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.