Python Selenium three kinds of wait way interpretation

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:

?
1234567891011 #-*-coding:utf-8-*- from selenium Import webdriver from time import sleep  driver = webdriver. Firefox () driver.get ( ' Https://huilansame.github.io '  sleep ( 3 # Force wait for 3 seconds to execute next   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:

?
123456789 #-*-coding:utf-8-*- from selenium import webdriver  driver = webdriver. Firefox () driver.implicitly_wait ( 30 # recessive wait, maximum wait 30 seconds driver.get ( ' Https://huilansame.github.io ' )  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:

?
12345678910111213141516 # -*- coding: utf-8 -*-from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Bydriver = 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 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.

We mainly use the Webdriverwait class and the Expected_conditions module, the following bloggers take a closer look at these two modules:

Webdriverwait

The Webdriverwait class of the wait module is the dominant wait class, which first looks at what parameters and methods it has:

?
1234567891011121314151617 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

Look at the above content is basically clear, the call method is as follows:

Webdriverwait (driver, timeout, frequency, ignore exception). Until (executable method, information returned at time of timeout)

It is important to note here that the executable method parameter in until or Until_not, many people pass in the Webelement object, as follows:

Webdriverwait (Driver). Until (driver.find_element_by_id (' kw ')) # error

This is the wrong use, the parameters here must be called, that is, this object must have the __call__ () method, otherwise it will throw an exception:

TypeError: ' xxx ' object is not callable

Here you can use the various conditions in the Expected_conditions module provided by selenium, or you can use the Webelement is_displayed (), is_enabled (), is_selected () method, Or you can do it in your own package, so let's take a look at what the selenium offers:

Expected_conditions

?
123456789101112131415161718192021222324252627282930313233343536373839404142 expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件:selenium.webdriver.support.expected_conditions(模块) 这两个条件类验证title,验证传入的参数title是否等于或包含于driver.titletitle_istitle_contains这两个人条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, ‘kw‘)顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行presence_of_element_locatedpresence_of_all_elements_located这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement第一个和第三个其实质是一样的visibility_of_element_locatedinvisibility_of_element_locatedvisibility_of这两个人条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的valuetext_to_be_present_in_elementtext_to_be_present_in_element_value这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElementframe_to_be_available_and_switch_to_it这个条件判断是否有alert出现alert_is_present这个条件判断元素是否可点击,传入locatorelement_to_be_clickable 这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组第三个传入WebElement对象以及状态,相等返回True,否则返回False第四个传入locator以及状态,相等返回True,否则返回Falseelement_to_be_selectedelement_located_to_be_selectedelement_selection_state_to_beelement_located_selection_state_to_be最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了staleness_of

The above is all 17 condition, with until, until_not combination can achieve a lot of judgments, if you can flexibly package, will greatly improve the stability of the script.

Python Selenium three kinds of wait way interpretation

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.