Home >Backend Development >Python Tutorial >Interpretation of three waiting methods in Python selenium
I found that too many people don’t know how to wait. The blogger really couldn’t help but tell everyone about the necessity of waiting today.
Many people ask in the group that this drop-down box cannot be located, that pop-up box cannot be located... various types of cannot be located, in fact, in most cases there are two problems: 1. There is a frame, 2 without waiting. As everyone knows, what is the order of magnitude of the running speed of your code, and what is the order of magnitude of the browser's loading and rendering speed? It's like the Flash and Alto Man making an appointment to fight monsters, and then after the Flash came back from the fight, he asked Alto Man about you. Why are you still putting on your shoes and not going out? 10,000 alpacas fly through the center of the game. Bullying me for being slow, I don’t want to play with you anymore. I throw an exception and give up my pick.
So how can we take care of the slow loading speed of Aotuman? There is only one way, and that is to wait. Speaking of waiting, there are three ways to wait, and let the blogger explain them one by one:
1. Forced waiting
The first and most simple and crude way is The first way is to force the wait for sleep(xx), forcing the Flash to wait for xx time. Regardless of whether Bumpman can keep up with the speed or has arrived early, he must wait for 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) # 强制等待3秒再执行下一步 print driver.current_url driver.quit()
This is called forced waiting. Regardless of whether your browser has finished loading, the program has to wait for 3 seconds, 3 Once the second is up, continue executing the following code. This is very useful for debugging. Sometimes you can wait like this in the code, but it is not recommended to always use this waiting method. It is too rigid and seriously affects the execution speed of the program.
2. Implicit waiting
The second method is called implicit waiting, implicitly_wait(xx), the meaning of implicit waiting is: The Flash and Bumpman agreed Okay, no matter where the Flash goes, he has to wait for BB Man for xx seconds. If BB Man comes within this time, the two of them will immediately set off to fight the monsters. If BB Man does not arrive within the specified time, the Flash himself Go, then naturally wait for Aotuman to throw an exception for you.
Look at the code:
# -*- coding: utf-8 -*- from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(30) # 隐性等待,最长等30秒 driver.get('https://huilansame.github.io') print driver.current_url driver.quit()
Invisible waiting is to set a maximum waiting time. If the web page is loaded within the specified time, it will be executed. Next step, otherwise wait until the time expires and then execute the next step. Note that there is a drawback here, that is, the program will wait for the entire page to be loaded. That is, usually when you see that the small circle in the browser tab bar no longer rotates, it will execute the next step. But sometimes the elements you want on the page The loading has been completed a long time ago, but because some js and other things are very slow, I still have to wait until the page is completely completed before taking the next step. I want to wait for the element I want to come out and then do the next step. What should I do? There is a way, it depends on another waiting method provided by Selenium - explicit wait wait.
It should be noted that implicit waiting works for the entire driver cycle, so it only needs to be set once. I have seen some people use implicit waiting as sleep, and it can be used anywhere. Take a look...
3. Explicit wait
The third way is to wait explicitly, WebDriverWait, with the until() and until_not() methods of this class, it You can wait flexibly based on judgment conditions. Its main meaning is: the program takes a look every xx seconds. If the condition is established, execute the next step. Otherwise, continue to wait until the set maximum time is exceeded, and then throw a TimeoutException.
Let’s look at a code example first:
# -*- 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) # 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者 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 up implicit wait and explicit wait, in other operations , the implicit wait plays a decisive role, and the explicit wait plays the main role in WebDriverWait.., but it should be noted that the longest waiting time depends on the larger of the two, in this example it is 20, if the implicit wait Waiting time > Explicit waiting time, then the maximum waiting time of the code is equal to the implicit waiting time.
We mainly use the WebDriverWait class and the expected_conditions module. The following blogger will take a closer look at these two modules:
WebDriverWait
wait module The WebDriverWait class is an explicit waiting class. Let’s first take a look at what parameters and methods it has:
selenium.webdriver.support.wait.WebDriverWait(类) __init__ driver: 传入WebDriver实例,即我们上例中的driver timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间) poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒 ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常, 则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。 until method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False message: 如果超时,抛出TimeoutException,将message传入异常 until_not 与until相反,until是当某元素出现或什么条件成立则继续执行, until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。 method message
After reading the above content, it is basically clear, the calling method As follows:
WebDriverWait(driver, timeout duration, calling frequency, ignore exception).until (executable method, information returned when timeout)
What needs special attention here is until Or the executable method method parameter in until_not, many people pass in the WebElement object, as follows:
WebDriverWait(driver, 10).until(driver.find_element_by_id('kw')) # Error
This is wrong usage. The parameters here must be callable, that is, this object must have a __call__() method, otherwise an exception will be thrown:
TypeError: 'xxx' object is not callable
Here, you can use various conditions in the expected_conditions module provided by selenium, or you can use WebElement's is_displayed(), is_enabled(), is_selected() methods, or encapsulate it yourself All methods are available, then let’s take a look at the conditions provided by selenium:
expected_conditions
expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件: selenium.webdriver.support.expected_conditions(模块) 这两个条件类验证title,验证传入的参数title是否等于或包含于driver.title title_is title_contains 这两个人条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, 'kw') 顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行 presence_of_element_located presence_of_all_elements_located 这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement 第一个和第三个其实质是一样的 visibility_of_element_located invisibility_of_element_located visibility_of 这两个人条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value text_to_be_present_in_element text_to_be_present_in_element_value 这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement frame_to_be_available_and_switch_to_it 这个条件判断是否有alert出现 alert_is_present 这个条件判断元素是否可点击,传入locator element_to_be_clickable 这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组 第三个传入WebElement对象以及状态,相等返回True,否则返回False 第四个传入locator以及状态,相等返回True,否则返回False element_to_be_selected element_located_to_be_selected element_selection_state_to_be element_located_selection_state_to_be 最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了 staleness_of
The above are all 17 conditions, which can be combined with until and until_not to achieve many judgments. If they can be flexibly encapsulated by themselves, the stability of the script will be greatly improved.
I will share these contents today. If you have any questions, please leave a message to me. I hope it can help students in need. Thank you all for your support of this site!
For more articles related to interpretation of Python selenium’s three waiting methods, please pay attention to the PHP Chinese website!