美文网首页
selenium的三种等待方式

selenium的三种等待方式

作者: E术家 | 来源:发表于2023-07-18 09:25 被阅读0次

sleep 强制等待

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get("http://localhost/selenium/myForm.html")

username_input = browser.find_element(by=By.ID, value="id01")
username_input.send_keys("aaa")

time.sleep(10) # 强制停止10s

password_input = browser.find_element(by=By.NAME, value="pwd")
password_input.send_keys("123456")

implicitly_wait 隐性等待

from selenium import webdriver

browser = webdriver.Chrome()
browser.implicitly_wait(3) # 最长等待时间
browser.get("http://localhost/selenium/myForm.html")

WebDriverWait 显性等待

一般结合until()或者until_not()expected_conditions类使用

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select

browser = webdriver.Chrome()  # 创建浏览器对象
browser.get("http://localhost/selenium/myForm.html")

timer = WebDriverWait(driver=browser, timeout=3) # 生成WebDriverWait对象,设置超时时间
ec_obj = EC.presence_of_element_located(locator=(By.XPATH, '//*[@id="id_sub123"]')) # 生成ec对象,判断元素是否被加到了 dom 树里
try:
    sub_btn = timer.until(ec_obj) # 尝试等待元素出现
except NoSuchElementException:
    browser.save_screenshot(filename='./Screenshots/error_useTimer.png') # 元素不存在时截图
except TimeoutException:
    browser.save_screenshot(filename='./Screenshots/error_timeout.png') # 超时时截图
else:
    sub_btn.click()
    browser.back()
finally:
    pass 

expected_conditions的常用方法

方法 说明
title_is 判断当前页面的 title 是否完全等于(==)预期字符串,返回布尔值
title_contains 判断当前页面的 title 是否包含预期字符串,返回布尔值
presence_of_element_located 判断元素是否被加到了 dom 树里(注意,加载到dom树中,并不代表这个元素可见)
presence_of_all_elements_located 判断是否至少有 1 个元素存在于 dom 树中。举例:如果页面上有 n 个元素的 class 都是’wp’,那么只要有 1 个元素存在,这个方法就返回 True
visibility_of_element_located 判断元素是否可见
visibility_of 同visibility_of_element_located方法,只是visibility_of_element_located方法参数为locator,这个方法参数是 定位后的元素
text_to_be_present_in_element 判断某个元素中的 text 是否 包含 了预期的字符串
text_to_be_present_in_element_value 判断某个元素中的 value 属性是否包含 了预期的字符串
frame_to_be_available_and_switch_to_it 判断该 frame 是否可以 switch进去,如果可以的话,返回 True 并且 switch 进去,否则返回 False
invisibility_of_element_located 判断某个元素中是否不存在于dom树或不可见
element_to_be_clickable 判断某个元素中是否可见并且可点击
staleness_of 等某个元素从 dom 树中移除,返回 True或 False
element_to_be_selected 判断某个元素是否被选中了,一般用在下拉列表
element_selection_state_to_be 判断某个元素的选中状态是否符合预期
element_located_selection_state_to_be 跟上面的方法作用一样,只是上面的方法传入定位到的 element,而这个方法传入 locator
alert_is_present 判断页面上是否存在 alert

相关文章

  • python + Selenium合集

    python + Selenium 合集 selenium原理,点这里selenium 三种等待方式,点这里~ 五...

  • selenium三种等待方式

    selenium三种等待方式[https://www.cnblogs.com/ctltest/p/14480682...

  • selenium 三种等待方式

    selenium 的三种等待方式 上面的隐式等待包括:WebDriverWait 默认每 500 毫秒调用一次 E...

  • selenium三种等待方式

    随着对selenium不断的深入学习,在网上搜索的资料越多,才发现自己掌握的只是冰山一角。 1、强制等待 用法:t...

  • selenium三种等待方式

    作者:Gakki 前言 在浏览器加载一个页面时,页面内得元素可能是在不同的时间载入的,这会加大定位元素的困难程度,...

  • Selenium的三种等待方式

    直接等待 time.sleep(1),强制等待线程休眠一定时间 使用上简单粗暴 以灵题库网站账号密码登录(http...

  • Selenium 三种等待元素出现的方式

    Selenium 等待元素出现的方式有以下三种1、显式等待通俗点说,就是死等,很死板不灵活的等待。即在指定的时间内...

  • selenium 延迟等待的三种方式

    1、最直接普通的方式:这个是设置固定的等待时间 Thread.sleep(1000); 2、隐式等待方式(impl...

  • Selenium三种等待方式的使用

    UI自动化测试,大多都是通过定位页面元素来模拟实际的生产场景操作。但在编写自动化测试脚本中,经常出现元素定位不到的...

  • 关于Selenium里等待的理解

    关于Selenium里等待的理解 首先使用selenium做自动化测试时有时需要等待元素的加载完成,常用的等待方式...

网友评论

      本文标题:selenium的三种等待方式

      本文链接:https://www.haomeiwen.com/subject/dclgudtx.html