广州金融网站设计wordpress 淘宝客主题
news/
2025/9/28 8:09:03/
文章来源:
广州金融网站设计,wordpress 淘宝客主题,seo关键词词库,wordpress微信机器人下载地址#x1f345; 视频学习#xff1a;文末有免费的配套视频可观看 #x1f345; 点击文末小卡片#xff0c;免费获取软件测试全套资料#xff0c;资料在手#xff0c;涨薪更快 time.sleep(3) 固定等待3秒
driver.implicitly_wait(10) 隐性的等待#xff0c;对应全局
WebD… 视频学习文末有免费的配套视频可观看 点击文末小卡片免费获取软件测试全套资料资料在手涨薪更快 time.sleep(3) 固定等待3秒
driver.implicitly_wait(10) 隐性的等待对应全局
WebDriverWait( driver, timeout).until(‘有返回值的__call__()方法或函数’) 显性的等待对应到元素
一、time.sleep(seconds) 固定等待
import time
time.sleep(3) #等待3秒
time.sleep(seconds) seconds参数为整数单位秒。
它是Python的time提供的休眠方法。
常用于短时间的等待为了自动测试用例的执行效率固定等待的时间需要控制在3秒内。在用例中尽量少用固定等待。
二、智能隐性的等待implicitly_wait回应超时等待
driver.implicitly_wait(time_to_wait)
回应超时等待隐性的设置后对应的是全局如查找元素。
driver.implicitly_wait(10) # 设置全局隐性等待时间单位秒
每次driver执行 找不到元素都会等待设置的时间它的值设置的过长对用例执行效率有很大的影响必须在执行完成之后还原回来。driver.implicitly_wait() 要慎之又慎的使用。
driver对它的默认值为0driver.implicitly_wait(0)能还原隐性等待的设置时间。
三、智能显性等待WebDriverWait
WebDriverWait(driver, timeout, poll_frequency0.5, ignored_exceptionsNone)
参数说明
driver 为webdriver驱动timeout 最长超时时间单位(秒)poll_frequency 循环查找元素每次间隔的时间默认0.5秒ignored_exceptions 超时后需要输出的异常信息
WebDriverWait()下面有两个方法可用until()和until_not()
WebDriverWait(driver, timeout).until(method, message)
method 函数或者实例__call__()方法返回True时停止否则超时后抛出异常。
参数说明
method 在等待时间内调用的方法或者函数该方法或函数需要有返回值并且只接收一个参数driver。 message 超时时抛出TimeoutException将message传入异常显示出来 WebDriverWait(driver, timeout).until_not(method, message)
于上面的until() 相反until_not 中的method函数或者实例__call__()方法返回False结束否则抛出异常。
until方法使用的method 的函数或者类__call()__方法详解
函数我们一般采用匿名函数lambda 。
lambda driver:driver.find_element(定位元素) # 当定位的元素时为True无元素时为False。如示例1、2
WebDriverWait示例1
WebDriverWait(driver,5).until(lambda driver:driver.find_element_by_id(query)) 5秒内等待元素(idquery)出现lambda driver:driver.find_element_by_id(query) 为一个匿名函数只有一个driver参数返回的是查找的元素对象。
WebDriverWait示例2
WebDriverWait(driver, 5).until_not(lambda driver:driver.find_element_by_name(query))
定义类中的__call()__方法。
class wait_element(object):def __init__(self, locator):self.locator locatordef __call__(self, driver):return driver.find_element(self.locator)WebDriverWait(driver, 5).until(wait_element((By.ID, query))) # 等待元素出现
WebDriverWait(driver, 5).until_not(wait_element((By.ID, query))) # 等待元素消失
wait_element类中__init__()方法接收需要定位的元素__call__()方法中只能有唯一变量driver并且返回元素对象。
这样做是是不是很麻烦其实selenium提供的一个库进行操作expected_conditions库。引入位置
from selenium.webdriver.support import expected_conditions as ec它囊括了我们需要使用等待的所有情况。
四、expected_conditions 类库
from selenium.webdriver.support import expected_conditions as ec # 引入包
下面示例都是以搜狗搜索首页为例。
方法中参数说明 locator(By.ID, id) 表示使用By方法定位元素的元组element表示获取的webElement元素对象。
ec.title_is(‘title’) 判断页面标题等于title
ec.title_contains(‘title’) 判断页面标题包含title
WebDriverWait(driver, 10).until(ec.title_is(搜狗搜索引擎 - 上网从搜狗开始)) # 等待title 于参数相等
WebDriverWait(driver, 10).until(ec.title_contains(搜狗搜索引擎)) # 等待title 包含 参数的内容
ec.presence_of_element_located(locator) 等待locator元素是否出现
ec.presence_of_all_elements_located(locator) 等待所有locator元素是否出现
WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, query)))
WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, query)))
ec.visibility_of_element_located(locator) 等待locator元素可见
ec.invisibility_of_element_located(locator) 等待locator元素隐藏
ec.visibility_of(element) 等待element元素可见
WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, query)))
WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, query)))
ec.text_to_be_present_in_element(locator,text) 等待locator的元素中包含text文本
ec.text_to_be_present_in_element_value(locator,value) 等待locator元素的value属性为value
WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element((By.ID, erwx), 搜索APP)) # 等待元素中包含搜索APP文本
WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element_value((By.ID, query),selenium)) # 等待元素的值为 selenium一般用于输入框
ec.frame_to_be_available_and_switch_to_it(locator) 等待frame可切入
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.ID, frame)))
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it(frameid))
ec.alert_is_present() 等待alert弹出窗口出现
WebDriverWait(driver, 10).until(ec.alert_is_present())
ec.element_to_be_clickable(locator) 等待locator元素可点击
WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, kw)))
等待元素被选中一般用于复选框,单选框
ec.element_to_be_selected(element) 等待element元素是被选中
ec.element_located_to_be_selected(locator) 等待locator元素是被选中ec.element_selection_state_to_be(element, is_selected) 等待element元素的值被选中为is_selected(布尔值)
ec.element_located_selection_state_to_be(locator,is_selected) 等待locator元素的值是否被选中is_selected(布尔值)
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec # 引入包
from selenium.webdriver.common.by import Bydriver webdriver.Chrome()
driver.maximize_window()
driver.get(rhttps://www.baidu.com/)
driver.find_element_by_link_text(设置).click()
WebDriverWait(driver, 3).until(ec.element_to_be_clickable((By.LINK_TEXT, 搜索设置))) # 等待搜索可点击不可缺少
driver.find_element_by_link_text(搜索设置).click()
element driver.find_element_by_id(s1_1)
WebDriverWait(driver, 2).until(ec.element_to_be_selected(element)) # element被选中
WebDriverWait(driver, 10).until(ec.element_located_to_be_selected((By.ID, SL_0))) # id’SL_0’ 被选中
WebDriverWait(driver, 10).until(ec.element_selection_state_to_be(element,True )) # element 被选中
WebDriverWait(driver, 10).until(ec.element_located_selection_state_to_be((By.ID, SL_1), False)) # id’SL_1’不被选中
time.sleep(3)
driver.quit()
五、什么时候使用等待
固定等待sleep与隐性等待implicitly_wait尽量少用它会对测试用例的执行效率有影响。
显性的等待WebDriverWait可以灵活运用什么时候需要用到
1、页面加载的时候确认页面元素是否加载成功可以使用WebDriverWait
2、页面跳转的时候等待跳转页面的元素出现需要选一个在跳转前的页面不存在的元素
3、下拉菜单的时候如上百度搜索设置的下拉菜单需要加上个时间断的等待元素可点击
4、页面刷新的时候
总之页面存在改变的时候页面上本来没的元素然后再出现的元素
同时在这我为大家准备了一份软件测试视频教程含面试、接口、自动化、性能测试等就在下方需要的可以直接去观看。 字节大佬一周讲完自动化测试项目实战这套教程是怎么称霸B站的【2024最新版】
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/920345.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!