English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Interpretation of Three Waiting Methods in Python Selenium

Found that too many people do not know how to use waiting, the blogger can't help but want to talk about the necessity of waiting today.

Many people in the group ask, this dropdown box cannot be located, that pop-up box cannot be located... all kinds of unlocatable, in fact, most of the time it is just two problems:1 with frame,2 No wait was added. But did you know what level your code runs at, and what level the browser loads and renders at? It's like Flash and Metamorpho making an appointment to fight a monster, and then Flash comes back after fighting and asks Metamorpho why he is still in his shoes and hasn't left yet? Metamorpho has a thousand giraffes running through his mind in a heartbeat, thinking he is too slow and not playing with him anymore, throws an exception and gives up the challenge.

So how can we take care of Metamorpho's slow loading speed? There is only one way, that is, to wait. When it comes to waiting, there are three ways to wait, and let the blogger explain one by one:

1. Forced wait

The first and simplest brute-force method is to use forced wait sleep(xx), forcing Flash to wait for xx seconds, regardless of whether Metamorpho can keep up with the speed or has already arrived early, he must wait for xx seconds.

See the code:

# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get('https://huilansame.github.io
sleep(3) # Forced wait3seconds before executing the next step
print driver.current_url
driver.quit()

This is called forced wait, regardless of whether the browser has finished loading, the program must wait.3seconds,3Once a second has passed, continue executing the following code, which is very useful for debugging. Sometimes you can also wait like this in the code, but it is not recommended to use this waiting method all the time. It is too rigid and seriously affects the execution speed of the program.

2. Implicit wait

The second method is called implicit wait, implicitly_wait(xx). The meaning of implicit wait is: Flash and Metamorpho make an agreement that no matter where Flash goes, he has to wait for Metamorpho xx seconds. If Metamorpho arrives within this time, both of them will immediately set off to fight the monster. If Metamorpho does not arrive within the specified time, Flash will go alone. Naturally, he will wait for Metamorpho to throw an exception to you.

See the code:

# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(30) # Implicit wait, maximum wait time30 seconds
driver.get('https://huilansame.github.io
print driver.current_url
driver.quit()

Implicit wait sets a maximum waiting time. If the web page is loaded within the specified time, the next step is executed; otherwise, it continues to wait until the time is up, and then execute the next step. Note that there is a drawback here, that is, the program will keep waiting for the entire page to load, that is, in general, you will see the small circle in the browser tab stop rotating, and then execute the next step. But sometimes the elements you want are already loaded, but because some js things are particularly slow, I still have to wait for the entire page to be completed before executing the next step. I want to execute the next step after the element I want appears. Is there a way? There is a way, which depends on another waiting method provided by selenium - explicit wait wait.

It should be particularly noted that implicit wait takes effect for the entire driver's cycle, so it can be set only once. I have seen some people use implicit wait as sleep... everywhere...

3. Explicit wait

The third method is explicit wait, WebDriverWait, combined with the until() and until_not() methods of this class, can flexibly wait according to the judgment conditions. Its main meaning is: the program looks at every xx seconds, if the condition is met, then execute the next step, otherwise continue waiting, until the set maximum time is exceeded, then throw TimeoutException.

Let's take a look at a code example:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.implicitly_wait(10) # Implicit wait and explicit wait can be used at the same time, but attention should be paid: the longest waiting time is taken from the larger of the two
driver.get('https://huilansame.github.io
locator = (By.LINK_TEXT, 'CSDN')
try:
  WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
  print driver.find_element_by_link_text('CSDN').get_attribute('href')
finally:
  driver.close()

In the above example, we set implicit and explicit waits, in other operations, implicit wait plays a decisive role, in WebDriverWait.. explicit wait plays a major role, but it should be noted that: the longest waiting time depends on the larger of the two, in this case it is20, if the implicit wait time is greater than the explicit wait time, then the longest waiting time of this line of code is equal to the implicit wait time.

We mainly use the WebDriverWait class and the expected_conditions module, let's take a detailed look at these two modules with the blogger:

WebDriverWait

The WebDriverWait class in the wait module is an explicit wait class, let's take a look at what parameters and methods it has:

selenium.webdriver.support.wait.WebDriverWait(class)
__init__
  driver: Pass in the WebDriver instance, that is, the driver in our previous example
  timeout: Timeout time, the longest time to wait (considering implicit wait time as well)
  poll_frequency: The interval time for calling the method in until or until_not, the default is 0.5seconds
  ignored_exceptions: Exceptions to be ignored, if this tuple of exceptions is thrown during the call to until or until_not
      then do not interrupt the code, continue to wait, if the exception thrown is not in this tuple, then interrupt the code, throw an exception. The default is NoSuchElementException.
until
  method: During the waiting period, this method passed in is called at regular intervals until the return value is not False
  message: If a timeout occurs, throw TimeoutException, pass message into the exception
until_not is the opposite of until, until is continued execution when an element appears or a condition is met
    until_not continues execution when an element disappears or a condition does not hold, the parameters are the same, no longer described.
  method
  message

After reading the above content, it is basically clear, the calling method is as follows:

WebDriverWait(driver, timeout, polling frequency, ignored exceptions).until(executable method, timeout returns information)

Here is a special point to note that the executable method parameter in until or until_not, many people pass in a WebElement object, as follows:

WebDriverWait(driver, 10).until(driver.find_element_by_id('kw')) # Error

This is incorrect usage, the parameters must be callable, that is, the object must have a __call__() method, otherwise an exception will be thrown:

TypeError: 'xxx' object is not callable

Here, you can use various conditions provided by the selenium expected_conditions module, or use the is_displayed()、is_enabled()、is_selected() methods of WebElement, or use your own encapsulated methods, so let's take a look at the conditions provided by selenium next:

expected_conditions

expected_conditions is a module of selenium, which contains a series of conditions that can be used for judgment:
selenium.webdriver.support.expected_conditions(module)
These two condition classes verify the title, checking whether the passed parameter title is equal to or contains driver.title
title_is
title_contains
These two conditions verify whether the element appears, and the parameters passed are all tuple type locators, such as (By.ID, 'kw')
As the name suggests, one passes as soon as a matching element is loaded; the other must load all matching elements
presence_of_element_located
presence_of_all_elements_located
These three conditions verify whether the element is visible, the first two pass parameters of tuple type locator, and the third passes WebElement
The first and third are essentially the same
visibility_of_element_located
invisibility_of_element_located
visibility_of
These two conditions determine whether a certain text appears in a certain element, one checks the element's text, and the other checks the element's value
text_to_be_present_in_element
text_to_be_present_in_element_value
This condition determines whether the frame can be inserted, and it can accept a tuple of locator or directly pass the locator method: id, name, index, or WebElement
frame_to_be_available_and_switch_to_it
This condition judges whether an alert appears
alert_is_present
This condition judges whether an element is clickable, passing in locator
element_to_be_clickable
These four conditions judge whether an element is selected. The first condition passes in a WebElement object, and the second condition passes in a locator tuple
The third parameter passes in a WebElement object and state, returns True if equal, otherwise False
The fourth parameter passes in locator and state, returns True if equal, otherwise False
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
The last condition judges whether an element is still in the DOM, passes in a WebElement object, and can determine whether the page has been refreshed
staleness_of

Above are all17One condition, combined with until, until_not, can realize many judgments. If you can encapsulate it flexibly, it will greatly improve the stability of the script.

That's all for today. If you have any questions, please leave a message for me to communicate. I hope it can help those in need. Thank you everyone for your support of this site!

Statement: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the content suspected of infringement.)