Python-Selenium 显性等待之 expected_conditions 的使用

Windows 10 + Python 3.7 + selenium==3.141.0 + urllib3==1.26.2 + Google Chrome 120.0.6099.130 (64 位)

# -*- coding: UTF-8 -*-

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    # 谷歌浏览器位置
    CHROME_PATH = r'C:\Program Files (x86)\Google\Chromium\Application\chrome.exe'
    CHROMEDRIVER_PATH = r'C:\Program Files (x86)\Google\Chromium\chromedriver.exe'
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    # 谷歌浏览器驱动地址
    options.binary_location = CHROME_PATH
    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)

    driver.get('https://www.baidu.com')
    print(driver.title)

    wait = WebDriverWait(driver, 30, 0.5)
    # title_is(title)
    # 判断当前页面标题是否为title,title:期望的页面标题,返回 BOOL
    bool_element = wait.until(expected_conditions.title_is('百度一下,你就知道'))
    print(bool_element)

    # title_contains(title)
    # 判断当前页面标题是否包含title,title:期望的页面标题,返回 BOOL
    bool_element = wait.until(expected_conditions.title_contains('百度一下'))
    print(bool_element)

    # presence_of_element_located(locator)
    # 判断此定位的元素是否被加载到dom树里,并不代表元素一定可见,locator:元素的定位信息,返回 WebElement
    locator = (By.ID, 'su')
    element = wait.until(expected_conditions.presence_of_element_located(locator))
    print(element)

    # url_contains(url)
    # 判断页面网址中是否包含url,url:期望的页面网址,返回 BOOL
    bool_element = wait.until(expected_conditions.url_contains('www.baidu.com'))
    print(bool_element)

    # url_matches(pattern)
    # 判断当前页面的url是否满足字符串正则表达式匹配,返回布尔值
    bool_element = wait.until(expected_conditions.url_matches('.+baidu.+'))
    print(bool_element)

    # url_to_be(url)
    # 判断页面网址是否为url,url:期望的页面网址,返回 BOOL
    bool_element = wait.until(expected_conditions.url_to_be('https://www.baidu.com/'))
    print(bool_element)

    # url_changes(url)
    # 判断页面网址不是url ,url:期望的页面网址, 返回 BOOL
    bool_element = wait.until(expected_conditions.url_changes('www.cctv.com'))
    print(bool_element)

    # visibility_of_element_located(locator)
    # 判断此定位的元素是否被加载到dom树里,并且可见,宽度和高度都大于0,locator:元素的定位信息,返回 WebElement
    locator = (By.ID, 'su')
    element = wait.until(expected_conditions.visibility_of_element_located(locator))
    print(element)

    # visibility_of(element)
    # 判断此元素是否可见,element:所获得的元素,如果可见就返回这个元素,返回 WebElement
    ele = driver.find_element(By.ID, 'su')
    element = wait.until(expected_conditions.visibility_of(ele))
    print(element)

    # presence_of_all_elements_located(locator)
    # 判断此定位的一组元素是否至少存在一个,locator:元素的定位信息,返回 WebElements List
    locator = (By.CLASS_NAME, 'hotsearch-item')
    elements = wait.until(expected_conditions.presence_of_all_elements_located(locator))
    print(type(elements), elements)

    # visibility_of_any_elements_located(locator)
    # 判断此定位的一组元素至少有一个可见,locator:元素的定位信息,返回 WebElements List
    locator = (By.CLASS_NAME, 'lh')
    elements = wait.until(expected_conditions.visibility_of_any_elements_located(locator))
    print(type(elements), elements)

    # visibility_of_all_elements_located(locator)
    # 判断此定位的一组元素全部可见,locator:元素的定位信息,返回 WebElements List
    # (这个没找到样例)
    # locator = (By.TAG_NAME, 'li')
    # elements = wait.until(expected_conditions.visibility_of_all_elements_located(locator))
    # print(type(elements), elements)

    # text_to_be_present_in_element(locator, text_)
    # 判断此定位中是否包含text_的内容,locator:元素的定位信息,text_:期望的文本信息,返回 BOOL
    locator = (By.ID, 's-usersetting-top')
    text_ = '设置'
    bool_element = wait.until(expected_conditions.text_to_be_present_in_element(locator, text_))
    print(bool_element)

    # text_to_be_present_in_element_value(locator, text_)
    # 判断此定位中的value属性中是否包含text_的内容,locator:元素的定位信息,text_:期望的文本信息,返回 BOOL
    locator = (By.ID, 'su')
    text_ = '百度一下'
    bool_element = wait.until(expected_conditions.text_to_be_present_in_element_value(locator, text_))
    print(bool_element)

    # frame_to_be_available_and_switch_to_it(locator)
    # 判断定位的元素是否为frame,并直接切换到这个frame中,locator:元素的定位信息,返回 BOOL
    driver.get('https://mail.qq.com/')

    locator = (By.CLASS_NAME, 'QQMailSdkTool_login_loginBox_qq_iframe')
    bool_element = wait.until(expected_conditions.frame_to_be_available_and_switch_to_it(locator))
    print(bool_element)

    locator = (By.ID, 'ptlogin_iframe')
    bool_element = wait.until(expected_conditions.frame_to_be_available_and_switch_to_it(locator))
    print(bool_element)

    # 密码登录
    locator = (By.ID, 'switcher_plogin')
    element = wait.until(expected_conditions.visibility_of_element_located(locator))
    element.click()

    # invisibility_of_element_located(locator)
    # 判断定位的元素是否不可见,locator:元素的定位信息

    #  invisibility_of_element(element)
    # 判断此元素是否不可见,element:所获得的元素

    # element_to_be_clickable(locator)
    # 判断所定位的元素是否可见且可点击,locator:元素的定位信息
    locator = (By.ID, 'login_button')
    element = wait.until(expected_conditions.element_to_be_clickable(locator))
    print(element)
    element.click()
    driver.switch_to.default_content()

    # staleness_of(element)
    # 判断此元素是否不可用,element:所获得的元素

    # element_to_be_selected(element)
    # 判断该元素是否被选中,element:所获得的元素,一般用在下拉列表
    driver.get('https://member.neea.cn/register')
    ele = driver.find_element(By.CSS_SELECTOR, '#signup_form_select > option:nth-child(1)')
    bool_element = wait.until(expected_conditions.element_to_be_selected(ele))
    print(bool_element)

    # element_located_to_be_selected(locator)
    # 判断定位的元素是否被选中,locator:元素的定位信息
    locator = (By.CSS_SELECTOR, '#signup_form_select > option:nth-child(1)')
    bool_element = wait.until(expected_conditions.element_located_to_be_selected(locator))
    print(bool_element)

    # element_selection_state_to_be(element, Boolean)
    # 判断该元素被选中状态是否和期望状态相同,element:所获得的元素,Boolean:期望的状态(True/False)
    # 测试 单选框 或 复选框
    element = driver.find_element(By.ID, 'female')
    is_selected = False
    bool_element = wait.until(expected_conditions.element_selection_state_to_be(element, is_selected))
    print(bool_element)

    # element_located_selection_state_to_be(locator,Boolean)
    # 判断定位的元素被选中状态是否和期望状态相同,locator:元素的定位信息,Boolean:期望的状态(True/False)
    # 测试 单选框
    locator = (By.ID, 'male')
    is_selected = True
    bool_element = wait.until(expected_conditions.element_located_selection_state_to_be(locator, is_selected))
    print(bool_element)

    # number_of_windows_to_be(num)
    # 判断当前浏览器页签数量是否为num,num:期望的页签数量
    num_windows = 2
    bool_element = wait.until(expected_conditions.number_of_windows_to_be(num_windows))
    print(bool_element)

    # new_window_is_opened(handles)
    # 判断此handles页签不是唯一打开的页签,handles:页签
    current_handles = driver.window_handles
    bool_element = wait.until(expected_conditions.new_window_is_opened(current_handles))
    print(bool_element)

    # alert_is_present()
    # 判断是否会出现alert窗口警报
    # w3school 的一个样例
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=hdom_alert')
    alert = wait.until(expected_conditions.alert_is_present())
    print(alert.text)
    alert.accept()

'''

实现代码位置:
Python37-32\Lib\site-packages\selenium\webdriver\support\expected_conditions.py

'''

'''
参考:
https://blog.csdn.net/hwijew/article/details/112858939
'''

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值