3.expected_conditions常用方法详解

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>简单的示例title>
    <div>
        <button onclick="alertUP()">alert弹窗button>
        <button onclick="confirmUP()">confirm弹窗button>
        <button onclick="promptUP()">prompt弹窗button>
    div>
    <div class="check-radio">
        单选:
        <label>
            <input type="radio" name="sex" value="男男"/>label>
        <label>
            <input type="radio" name="sex" value="女女"/>label>
    div>
    <div>
        多选:
        <label>
            <input type="checkbox" name="name" value="male"/> 张三
        label>
        <label>
            <input type="checkbox" name="name" value="female"/>李四
        label>
    div>
    <div>
        输入框:
        <label>
            <input name="name" value="" class="demo-class" placeholder="请输入名称"/>
        label>
        <label>
            <input name="name" value="" placeholder="请输入名称"/>
        label>

    div>
    <div id="to-baidu">
        新开标签页:
        <a href="https://www.baidu.com/" target="_blank">前往百度a>
    div>
    <div>
        下拉框:
        <label>
            <select>
                <option value="volvo">PHP中文网option>
                <option value="saab">百度option>
                <option value="opel">腾讯option>
                <option value="audi">新浪option>
            select>
        label>
    div>
    <div>
        删除dom里的元素:
        <button onclick="deleteToBaidu()">删除跳转到百度button>
    div>
    <div>
        iframe:
        <iframe id="mainContent" src="https://fsoufsou.com/" width="100%" height="100%">iframe>
    div>

head>
<body>

body>
html>
<script>
    function alertUP() {
        alert("这是一个alert弹窗")
    }

    function confirmUP() {
        const value = confirm("这是一个confirm弹窗")
        if (value) {
            alert("你选择的是【确定】")
        } else {
            alert("你选择的是【取消】")
        }
    }

    function promptUP() {
        const value = prompt("这是一个prompt弹窗")
        if (value) {
            alert("你输入的是:" + value)
        } else {
            alert("你选择的是【取消】")
        }
    }

    function deleteToBaidu() {
        const div = document.getElementById("to-baidu");
        div.remove();
    }
script>

期望条件(expected_conditions

selenium.webdriver.support.expected_conditions - Selenium 4.4 documentation

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

1.alert_is_present(判断弹框是否存在)

alert

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击alert按钮
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[1]/button[1]'))).click()
# TODO 判断是否存在弹窗
element = WebDriverWait(driver, 3, 1).until(expected_conditions.alert_is_present())
time.sleep(1)
# 接受弹窗
element.accept()
time.sleep(1)
driver.quit()

confirm

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击confirm按钮
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[1]/button[2]'))).click()
# TODO 判断是否存在弹窗
element = WebDriverWait(driver, 3, 1).until(expected_conditions.alert_is_present())
time.sleep(1)
# TODO 接受弹窗
# element.accept()
# TODO 拒绝弹窗
element.dismiss()
time.sleep(1)
driver.quit()

prompt

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击prompt按钮
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[1]/button[3]'))).click()
# TODO 判断是否存在弹窗
element = WebDriverWait(driver, 3, 1).until(expected_conditions.alert_is_present())
time.sleep(1)
# TODO 输入(这个过程貌似看不见输入的动作)
element.send_keys('123456')
# TODO 接受弹窗
element.accept()
# TODO 拒绝弹窗
# element.dismiss()
time.sleep(1)
driver.quit()

2.element_attribute_to_include(判断某个元素属性是否有存在)

from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[4]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检查locator元素是否存在name属性
element1 = WebDriverWait(driver, timeout=3).until(expected_conditions.element_attribute_to_include(locator, 'class'))
print(element1)
# TODO 检查locator元素是否存在name1属性,因为name1不存在,或报错:TimeoutException,可以通过捕获异常处理
element2 = WebDriverWait(driver, timeout=3).until(expected_conditions.element_attribute_to_include(locator, 'class1'))
print(element2)
driver.quit()

3.presence_of_element_located(定位元素,与find_element类似),同理presence_of_all_elements_located定位一组元素。这并不一定意味着该元素是可见的

选择要定位的元素,定位器是 (定位方式, 值) 这样的元组

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 定位元素
element = WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator))
# TODO 元素找到后,后面可以跟常用点击、输入等方法
element.click()
time.sleep(2)
driver.quit()

4.element_located_selection_state_to_be(判断元素是否选择,单选框、复选框)

期望定位元素并检查指定的选择状态是否处于该状态

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击复选框
WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator)).click()
# TODO 检查locator元素是否存在
# TODO element_located_selection_state_to_be的第二个参数是bool值
# TODO 上面点击了复选框,如果第二个值设为True,则返回True。如果如果第二个值设为False,则会报TimeoutException的异常
element1 = WebDriverWait(driver, timeout=3).until(
    expected_conditions.element_located_selection_state_to_be(locator, True))
print(element1)
time.sleep(2)
driver.quit()

5.element_selection_state_to_be(判断元素是否选择,单选框、复选框)

检查给定元素是否被选中的期望,用法和element_located_selection_state_to_be一样,第一个参数为元素定位的对象。这两种看自己情况用那种

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[1]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击复选框
web_driver = WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator))
element = WebDriverWait(driver, timeout=3).until(
    expected_conditions.element_selection_state_to_be(web_driver, True))
print("值:", element)
time.sleep(1)
driver.quit()

6.element_to_be_clickable(判断元素能否点击)。个人感觉和element_located_to_be_selected一样的

检查元素的期望是可见的并启用,以便您可以单击它。若不能点击就会报TimeoutException的异常

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[1]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# TODO 获取locator元素的name属性
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击复选框
WebDriverWait(driver, timeout=3).until(expected_conditions.element_to_be_clickable(locator)).click()
time.sleep(1)
driver.quit()

7.element_to_be_selected(判断下拉框是否选中)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[4]/label/select/option[2]')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# TODO 获取locator元素的name属性
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击下拉
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[4]/label/select/option[1]'))).click()
# TODO 定位第二个值
web_driver = WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator))
# TODO 选择第二个值
web_driver.click()
# TODO 判断选中的是否是第二个
element = WebDriverWait(driver, timeout=3).until(expected_conditions.element_to_be_selected(web_driver))
print(element)
time.sleep(1)
driver.quit()

8.frame_to_be_available_and_switch_to_it(检测iframe是否可用,可用就直接切入)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '//*[@id="mainContent"]')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检测iframe是否可用,可用就直接切入
element = WebDriverWait(driver, timeout=3).until(expected_conditions.frame_to_be_available_and_switch_to_it(locator))
print(element)
# TODO 在iframe里的输入框,输入123456
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '//*[@id="search-input"]'))).send_keys('123456')
time.sleep(1)
# TODO 切回到主页
driver.switch_to.default_content()
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[4]/label[1]/input'))).send_keys('123456')
time.sleep(1)
driver.quit()

9.invisibility_of_element和invisibility_of_element_located(检测是否不可见)

检测是否不可见,locator需要是一个不存在的定位,返回True,如果是一个存在的元素,报超时异常

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[4]/label[1]/input121')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检测是否不可见,locator需要是一个不存在的定位,返回True,如果是一个存在的元素,报超时异常
element = WebDriverWait(driver, timeout=3).until(expected_conditions.invisibility_of_element(locator))
print(element)
driver.quit()

10.new_window_is_opened(检测是否新增了一个窗口)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[5]/a')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 当前窗口数量
window_handles_1 = driver.window_handles
print(window_handles_1)
# TODO 点击一个新窗口
WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator)).click()
# TODO 检测此时的窗口数与window_handles相比是否多一个
element = WebDriverWait(driver, timeout=3).until(expected_conditions.new_window_is_opened(window_handles_1))
print(element)
# TODO 当前窗口数量
window_handles_2 = driver.window_handles
print(window_handles_2)
# TODO 切换回主窗口
time.sleep(1)
driver.switch_to.window(window_handles_2[0])
# TODO 切换到百度窗口
time.sleep(1)
driver.switch_to.window(window_handles_2[1])
time.sleep(1)
driver.quit()

11.number_of_windows_to_be(检测当前窗口数是否为期望值)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[5]/a')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 点击一个新窗口
WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator)).click()
# TODO 检测此时的窗口数是否为2
element = WebDriverWait(driver, timeout=3).until(expected_conditions.number_of_windows_to_be(2))
print(element)
time.sleep(1)
driver.quit()

12.staleness_of( 检测元素是否不在DOM中)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '//*[@id="to-baidu"]')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 元素是否在DOM中
element = WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located(locator))
# TODO 删除跳转到百度
WebDriverWait(driver, timeout=3).until(
    expected_conditions.presence_of_element_located(('xpath', '/html/body/div[7]/button'))).click()
# # TODO 检测元素是否不在DOM中
res = WebDriverWait(driver, timeout=3).until(expected_conditions.staleness_of(element))
print(res)
time.sleep(1)
driver.quit()

13.text_to_be_present_in_element(检查给定文本是否存在于指定元素中的期望)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检查locator元素里的文本值是否包含“单选”
element = WebDriverWait(driver, timeout=3).until(expected_conditions.text_to_be_present_in_element(locator, '单选'))
print(element)
driver.quit()

14.text_to_be_present_in_element_value(期望检查给定文本是否存在于元素的属性中)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检查locator元素class属性是否包含“check-radio”
element1 = WebDriverWait(driver, timeout=3).until(
    expected_conditions.text_to_be_present_in_element_attribute(locator, 'class', 'check-radio'))
# TODO 检查locator元素class属性是否包含“check”
element2 = WebDriverWait(driver, timeout=3).until(
    expected_conditions.text_to_be_present_in_element_attribute(locator, 'class', 'check'))
print(element1)
print(element2)
driver.quit()

15.text_to_be_present_in_element_value(检查给定文本是否存在于元素值中的期望)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 检查locator元素value属性是否包含“男”
element1 = WebDriverWait(driver, timeout=3).until(
    expected_conditions.text_to_be_present_in_element_value(locator, '男'))
print(element1)
driver.quit()

16.title_contains和title_is(检查页面标题的期望)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO 标题是否包含 简单
element1 = WebDriverWait(driver, timeout=3).until(expected_conditions.title_contains('简单'))
# TODO 标题是否等于简单的示例
element2 = WebDriverWait(driver, timeout=3).until(expected_conditions.title_is('简单的示例'))
print(element1)
print(element2)
driver.quit()

17.url_contains和url_to_be(检查当前 url 的期望)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO URL是否包含 selenium4_demo
print(driver.current_url)
element1 = WebDriverWait(driver, timeout=3).until(expected_conditions.url_contains('selenium4_demo'))
# TODO URL是否等于 file:///E:/PycharmProjects/selenium4_demo/test.html
element2 = WebDriverWait(driver, timeout=3).until(expected_conditions.url_to_be('file:///E:/PycharmProjects/selenium4_demo/test.html'))
print(element1)
print(element2)
driver.quit()

18.visibility_of_element_located和visibility_of_all_elements_located (检查元素是否存在于页面的 DOM 上并且可见的期望。可见性意味着元素不仅被显示,而且其高度和宽度都大于 0。 定位器 - 用于查找元素,一旦定位并可见,则返回 WebElement)

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

locator = ('xpath', '/html/body/div[2]/label[1]/input')
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get(r'E:\PycharmProjects\selenium4_demo\test.html')
# TODO URL是否包含 selenium4_demo
element1 = WebDriverWait(driver, timeout=3).until(expected_conditions.visibility_of_element_located(locator))
print(element1)
driver.quit()

你可能感兴趣的:(selenium自动化,python,显性等待,UI自动化,selenium)