Python Selenium three ways to wait

Source: Internet
Author: User
Tags set time



1. Forced wait
The first is also the most simple and rough one way is to force waiting for sleep (xx), the force of the Lightning, such as XX time, whether the bump can keep up with the speed, or has been early, 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://www.xuehu365.com’)
sleep (3) # Force to wait 3 seconds before executing the next step
print driver.current_url
driver.quit () 


This is called forced wait, regardless of whether your browser is loaded, the program will have to wait 3 seconds, 3 seconds to continue to execute the following code, as debugging is very useful, and sometimes can be in the code such as waiting, but not always use this kind of wait, too rigid, seriously affect program execution speed.



2. Hidden waiting
The second method is called the Hidden Wait, implicitly_wait (xx), the meaning of the hidden wait is: The flash and bump-man agreement, no matter where the flash to, all must wait for the bump man xx seconds, if the bump in this period of time to come, then two people immediately set off to play monsters, if the bump in the specified time, Then the flash himself, that nature will wait for you to throw an exception.
Look at the code:


#-*-coding: utf-8-*-
from selenium import webdriver
driver = webdriver.Firefox ()
driver.implicitly_wait (30) # implicit wait, wait up to 30 seconds
driver.get (‘https://www.xuehu365.com’)
print driver.current_url
driver.quit () 


The stealth wait is set to a maximum wait time, if the page load completes within the specified time, then the next step, otherwise wait until the time cutoff, and then perform the next step. Note Here is a disadvantage, that is, the program will always wait for the entire page to load complete, that is, generally you see the browser tab bar that small circle no longer go, will do the next step, but sometimes the page wants the element has been loaded completed, but because the individual JS and other things particularly slow, I still have to wait until the page is complete to perform the next step, and I want to wait for the element I want to come out and what to do next? There is a way, it depends on selenium provide another way of waiting--------explicit waiting wait.



What needs to be specifically explained is: The recessive waits for the whole driver the cycle all to have the function, so as long as set once can, I have seen someone to take the recessive wait as the sleep in use, walks everywhere to come ...



3. Explicit wait
The third method is the explicit wait, webdriverwait, with the class of until () and Until_not () methods, can be based on the conditions of judgment and flexibility to wait. Its main meaning is: The program every XX seconds to see a glance, if the condition is set up, then the next step, otherwise continue to wait until the maximum set time, and then throw timeoutexception.
Let's 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 note that the longest wait time is the greater of
driver.get (‘https://xuehu365.com’)
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 example above, we set the implicit wait and explicit wait, in other operations, the implicit wait plays a decisive role in webdriverwait. The dominant wait plays a major role, but it is important to note that the longest wait time depends on the larger of the two, in this case, 20, if the implicit wait time > Dominant wait time, the maximum wait time for the sentence code is equal to the hidden wait time.



Python Selenium three ways to wait


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.