Python+selenium Automation -8-setting waits for three waiting methods

Source: Internet
Author: User

If you encounter a webpage loaded with Ajax, the page element may not be loaded at the same time, this time, we need to set a wait condition, wait for the page element to complete, to avoid the occurrence of errors caused by the element not loaded.

Webdriver provides two types of wait: Display wait, implicit wait.

1. Display wait: webdriverwait () class
    • Show wait: Set a wait time and a condition, within the specified time, every time to see whether the condition is set up, if the program will continue to execute, otherwise, a timeout exception (timeoutexception) is prompted.
    • Typically, the webdriverwait class is used in conjunction with the expectedcondition class.
Instance:
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.Chrome()driver.get(‘https://www.baidu.com‘)# 设置浏览器:driver  等待时间:20swait = WebDriverWait(driver, 20)# 设置判断条件:等待id=‘kw‘的元素加载完成input_box = wait.until(EC.presence_of_element_located((By.ID, ‘kw‘)))# 在关键词输入:关键词input_box.send_keys(‘关键词‘)
Specific parameters and methods of webdriverwait:
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)    driver: 浏览器驱动    timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)    poll_frequency: 每次检测的间隔时间,默认是0.5秒    ignored_exceptions:超时后的异常信息,默认情况下抛出NoSuchElementException异常until(method,message=‘‘)    method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False    message: 如果超时,抛出TimeoutException,将message传入异常until_not(method,message=‘‘)    until_not 与until相反,until是当某元素出现或什么条件成立则继续执行,    until_not是当某元素消失或什么条件不成立则继续执行,参数也相同。    method    message
Expectedcondition
    • Expectedcondition criteria to be used in the evaluation:
  from Selenium.webdriver.support import expected_conditions as ec# determine if the title is consistent with the expected title_is# Determines whether the header contains the expected string title_contains# determines whether the specified element is loaded presence_of_element_located# determine if all elements are loaded Presence_of_all_elements_ located# determines whether an element is visible. Visible represents the element is not hidden, and the width and height of the element is not equal to 0, the passed parameter is the locatorvisibility_of_element_located# of the tuple type determines whether the element is visible, The passed-in parameter is the positioned element webelementvisibility_of# determines whether an element is not visible, or does not exist in the DOM tree invisibility_of_element_located# the text of the judging element Whether to include the expected string text_to_be_present_in_element# determine whether the element's value contains the expected string text_to_be_present_in_element_value# determine if the frame can be cut, You can pass in locator tuples or direct incoming targeting methods: ID, name, index, or webelementframe_to_be_available_and_switch_to_it# to determine if there are alert occurrences Alert_is_ present# determines whether an element can be clicked element_to_be_clickable# determine whether the element is selected, generally used in a drop-down list, passed in the Webelement object element_to_be_selected# Determines whether the element is selected element_located_to_be_selected# determines whether the selected state of the element is the same as expected, passed in parameters: the positioned element, equal returns TRUE, otherwise returns falseelement_selection_state _to_be# determines whether the selected state of an element is the same as expected, passed in parameters: element positioning, equality returns TRUE, otherwise falseelement_located_selection_state_to_be# determines whether an element is still in the DOM, Passing in the Webelement object, you can tell whether the page refreshed staleness_of  
The calling method is as follows:
WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)
2. Implicit wait
    • Implicitly_wait (XX): Set the wait time to xx seconds, wait for the element to load complete, if the time element is not loaded, throw a nosuchelementexception error.
    • Note: The implicit wait will work for the entire driver cycle, so just set it once.
from selenium import webdriverdriver = webdriver.Chrome()driver.implicitly_wait(30)  # 隐性等待,最长等30秒driver.get(‘https://www.baidu.com‘)print(driver.current_url)print(driver.title)
3. Force wait: Sleep ()
    • Force wait: Regardless of whether the browser element is loaded, the program waits 3 seconds and 3 seconds to continue executing the following code.
from selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()driver.get(‘https://www.baidu.com‘)sleep(3)  # 强制等待3秒再执行下一步print(driver.title)

Python+selenium Automation -8-setting waits for three waiting methods

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.