ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python_expected_conditions模块用法简介

2021-10-04 15:04:16  阅读:292  来源: 互联网

标签:10 Python driver EC expected import conditions until WebDriverWait


转载来源:https://www.cnblogs.com/dream66/p/12665563.html

一、expected_conditions模块是什么?

  • 是Selenium的一个子模块,selenium.webdriver.support.expected_conditions
  • 可以对网页上元素是否存在,可点击等等进行判断,一般用于断言或与WebDriverWait配合使用

二、expected_conditions模块简单应用

  • 2.1 WebDriverWait与expected_conditions配合使用实例一

import os
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

# 等待10s,等待过程中判断网页标题是否是"百度一下,你就知道"
# 如果是就继续执行后续代码,反之等待10s结束时报错
WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
  • 2.2 WebDriverWait与expected_conditions配合使用实例二

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

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
#等待10s,等待过程中如果定位到元素,就直接执行后续的代码,反之等待10s后报错误信息
element = WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw')))
element.send_keys( '新梦想软件测试' )
  • 2.3 unittest与expected_conditions配合使用实例

import time
import unittest
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

class TestDemo(unittest.TestCase):
    def setUp(self) :
        self.driver = webdriver.Chrome()
    def tearDown(self):
        time.sleep(2)
        self.driver.quit()

    def test_searchinputbox_is_visibility(self):
        self.driver.get('https://www.baidu.com')
        #EC.visibility_of()判断元素是否可见,如果可见就返回这个元素 
        self.assertTrue(EC.visibility_of(self.driver.find_element(By.ID,'kw')))

if __name__=='__main__':
    unittest.main()

实例小结:

  • 实例一与实例二中用到了显式等待 WebDriverWait类,该块不在此文中介绍;
  • 实例三中self.assertTrue()方法断言括号内的表达式返回值是否为ture,在python中代表true的为 非0、非空、true,而EC.visibility_of()方法中的定位方法能定位到元素就会返回一个对象,满足非空为true,所以断言会通过;
  • 注意EC.visibility_of()方法返回的对象非真实元素对象,所以不能执行如下代码:(正确方式参照实例二的写法)
element = EC.visibility_of(self.driver.find_element(By.ID,'kw'))
element.send_keys('newdream')

三、expected_conditions模块用法汇总

#判断当前页面的title是否精确等于预期,返回布尔值
WebDriverWait(driver,10).until(EC.title_is("百度一下,你就知道"))
#判断当前页面的title是否包含预期字符串,返回布尔值
WebDriverWait(driver,10).until(EC.title_contains('new'))
#判断当前页面的url是否精确等于预期,返回布尔值
WebDriverWait(driver,10).until(EC.url_contains('https://www.baidu.com'))
#判断当前页面的url是否包含预期字符串,返回布尔值
WebDriverWait(driver,10).until(EC.url_contains('baidu'))
#判断当前页面的url是否满足字符串正则表达式匹配,返回布尔值
WebDriverWait(driver,10).until(EC.url_matches('.+baidu.+'))
#判断元素是否出现,只要有一个元素出现,返回元素对象
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
#判断元素是否可见,返回元素对象
WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(By.ID,'kw')))
#判断元素是否包含指定文本,返回布尔值
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.NAME,'tj_trnews'),'新闻'))
#判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去
WebDriverWait(driver,10,).until(EC.frame_to_be_available_and_switch_to_it(By.xpath,'//iframe'))
#判断某个元素是否可见并且是可点击的,如果是的就返回这个元素,否则返回False
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,'tj_trnews')))
#判断某个元素是否被选中,一般用在下拉列表
WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.xpath,'//input[@type="checkbox"]')))
#判断页面上是否存在alert,如果有就切换到alert并返回alert的内容
WebDriverWait(driver,10).until(EC.alert_is_present())
#还有不少,用到了继续更新......

备注:以上整理大家要注意参数和返回值,部分参数是元素对象,部分是locator的元组,如(By.NAME,'tj_trnews')

标签:10,Python,driver,EC,expected,import,conditions,until,WebDriverWait
来源: https://www.cnblogs.com/ma-yi/p/15366390.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有