English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Many people ask in the group, this dropdown box cannot be located, that pop-up box cannot be located... all kinds of unlocatable. In fact, most of the time, there are only two problems:1 with frame,2 There is no waiting. Little does it know what level your code runs at, and what level the browser loads and renders at. It's like Flash and Shoumu agree to go fight the monster, and then Flash comes back and asks Shoumu why he is still wearing shoes and not leaving the house. Shoumu is overwhelmed with a thousand camels crossing his mind in a split second. He is bullied because he is slow, he won't play with you, and throws an exception and gives up the challenge.
So how can we take care of Shoumu'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 them one by one:
1. Forced wait
The first and simplest method is forced wait sleep(xx), forcing Flash to wait xx time, regardless of whether Shoumu can keep up with the speed or has arrived early, he must wait xx time.
Look at 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 loaded completely, the program must wait3seconds3Once the second hand reaches the mark, continue executing the code below, 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, as 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 Shoumu agree that no matter where Flash goes, he has to wait for Shoumu xx seconds. If Shoumu comes within this time, both of them will immediately set off to fight the monster. If Shoumu doesn't come within the specified time, Flash will go alone, and naturally wait for Shoumu to throw an exception for you.
Look at the code:
# -*- coding: utf-8 -*- from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(30) # Implicit wait, maximum wait30 seconds driver.get('https://huilansame.github.io print driver.current_url driver.quit()
Invisible waiting sets a maximum waiting time. If the web page is loaded within the specified time, the next step is executed. Otherwise, it waits until the time is up and then executes the next step. Note that there is a drawback here, that is, the program will keep waiting for the entire page to load, which is generally the case when you see the small circle in the browser tab stop spinning, and then execute the next step. But sometimes the elements that the page wants are already loaded, but because some JavaScript things are particularly slow, I still have to wait for the page to be fully loaded before executing the next step. What should I do if I want to execute the next step as soon as the elements I want appear? There is a way, and that is to look at another waiting method provided by Selenium - explicit waiting wait.
It should be specially pointed out that: implicit wait takes effect on the entire driver's cycle, so it can be set only once. I have seen some people use implicit wait as sleep, coming everywhere...
3. Explicit wait
The third method is explicit wait, WebDriverWait, which, combined with the until() and until_not() methods of this class, can wait flexibly according to the judgment conditions. Its main meaning is: the program looks at the condition every xx seconds, if the condition is met, then execute the next step, otherwise continue waiting, until exceeding the set maximum time, 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 should be 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()
}}2In the above example, we set implicit and explicit waiting times. In other operations, implicit waiting plays a decisive role, and explicit waiting plays a major role in WebDriverWait.. However, it should be noted that the longest waiting time depends on the larger of the two, which is the implicit waiting time in this example.
We mainly use the WebDriverWait class and the expected_conditions module. Below, the blogger will take you through these two modules in detail:
WebDriverWait
The WebDriverWait class in the wait module is an explicit wait class. Let's take a look at its parameters and methods first:
selenium.webdriver.support.wait.WebDriverWait (class) __init__ driver: Pass in the WebDriver instance, that is, the driver in our previous example timeout: The timeout time, the longest time to wait (considering the implicit waiting time) 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 the exception in the process of calling until or until_not is in this tuple, the code will not be interrupted and will continue to wait. If the exception thrown is outside this tuple, the code will be interrupted and the exception will be thrown. 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, TimeoutException is thrown, and the message is passed into the exception until_not is the opposite of until. until is executed when an element appears or a condition is met, while until_not is executed when an element disappears or a condition is not met. The parameters are the same, and will not be elaborated further. 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_message)
Here, it is especially important to pay attention to the executable method parameter in until or until_not, as many people pass in a WebElement object, as follows:
WebDriverWait(driver, 10until(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 encapsulate your own 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 conditions 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 an 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 one element that meets the condition is loaded; the other must load all elements that meet the condition presence_of_element_located presence_of_all_elements_located These three conditions verify whether an element is visible, the first two pass parameters of tuple type locator, and the third passes WebElement The first and the third are actually the same in essence visibility_of_element_located invisibility_of_element_located visibility_of These two conditions judge whether a certain text appears in a certain element, one judges the text of the element, and the other judges the value of the element text_to_be_present_in_element text_to_be_present_in_element_value This condition judges whether the frame can be switched to, and it can pass a locator tuple 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 the element is clickable, by passing in a locator element_to_be_clickable These four conditions judge whether the element is selected, the first one passes in a WebElement object, and the second one passes in a locator tuple The third one passes in a WebElement object and status, returns True if equal, otherwise False The fourth one passes in locator and status, 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, by passing in a WebElement object, it can be determined whether the page has been refreshed staleness_of
The above is all17A condition, combined with until and until_not, can realize many judgments. If you can encapsulate it flexibly, it will greatly improve the stability of the script.
The above is the detailed explanation of the three waiting methods of Python selenium introduced by the editor (must know), hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to you in time. Thank you very much for your support of the Yelling Tutorial website!
Declaration: The content of this article is from the Internet, 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 any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w. We welcome your feedback and are very grateful for your support of the Yelling Tutorial website!3Please report via email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.