Python+Selenium - 三种等待方式
2021/5/17 20:27:39
本文主要是介绍Python+Selenium - 三种等待方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
元素:存在 > 可见 > 可用
需要判断元素状态
等待方式1:强制等待 -- 辅助
设置等待几秒,就必须等待几秒
示例:
from time import sleep sleep(3)
强制等待3秒
等待方式2:智能等待(隐性等待) -- 辅助
implicity_wait
1.每个会话只调用一次
2.找元素、命令执行完成
示例:
from selenium import webdriver driver = webdriver.Chrome() driver.get("www.baidu.com") driver.implicitly_wait(10) #
智能等待10秒
等待方式3:显性等待 -- 常用
由两部分组成:等待(WebDriverWait类) + 条件(expected_condition)
WebDriverWait(driver, 超时时间, 查看周期=0.5).until(条件) #每0.5秒查看一次是否满足条件
WebDriverWait(driver, 超时时间, 查看周期=0.5).not_until(条件)
常用的三种元素判断方法
presence_of_element_located() #元素存在
visibility_of_element_located() #元素可用
element_to_be_clickable() #元素可点击
示例:
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium import webdriver driver = webdriver.Chrome() driver.get("www.baidu.com") wait = WebDriverWait(driver,20) #20秒后超时 loc = (By.ID,'kw') wait.until(EC.presence_of_element_located(loc)) #判断元素存在,这里不需要解包
这篇关于Python+Selenium - 三种等待方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python