
python自动化8 等待处理
发布日期:2023-05-26 14:15:28
浏览次数:2
分类:技术文章
本文共 1908 字,大约阅读时间需要 6 分钟。
为了保证脚本的稳定性,有时候需要引入等待时间,等待页面加载元素后再进行操作,selenium提供三种等待时间设置方式。
sleep():固定休眠时间设置
python的time包里提供了休眠方法sleep,导入包后就能使用:
sleep()方法以秒为单位,如果超时设置小于1秒,可以使用小数 import time time.sleep(0.5)implicitlyWait:隐式等待
implicitlyWait()方法比sleep0方法智能sleep()方法只能在一个固定的时间等待,而implicitlyWait()可以在一个时间范围内等待称为隐式等待
driver.implicitly_wait(100) element=driverfind_element_by_css_selector( “div.red box”)备注: 设置等待时间100s,页面上的元素5s后出现,只等待5s。不会等待100秒。 机制: 智能等待:在一个时间段内的等待 1.全局设置:只要在find_elements就会等待范围内等待; 2.每隔500ms在界面进行一次检查,如果检查到了就不等待,如果在设置时间内没检查到则报错。from selenium.webdriver.support.ui import WebDriverWaitclass WebDriver: def __init__(self,driver_path=config_u.get_driver_path): path = Service(driver_path) self.driver = webdriver.Firefox(service=path) self.mouse = ActionChains(self.driver) def find_element_xpath(self,xpath_path,key_word): self.driver.find_element(By.XPATH,xpath_path).send_keys(key_word) def element_implicitly(self,sec): #隐式等待 self.driver.implicitly_wait(sec) pass
WebDriverWait():显示等待
语法格式如下:
WebDriverWait(driver,timeout,poll_frequency=0.5,ignore_exceptions=None) driver: WebDriver的驱动程序(IE火狐,谷歌或远程); timeout: 最长超时时间,默认以秒为单位; poll_frequency: 休眠时间的间隔(步长)时间,默认为0.5秒(即每500毫秒扫描一次页面); ignore_exceptions:超时后的异常信息,默认情况下抛NoSuchElementException异常; 在一个时间段内的等待,只针对一个element生效。实例:在20s内直到找到element为止。
from selenium.webdriver.support.ui import WebDriverWaitclass WebDriver: def __init__(self,driver_path=config_u.get_driver_path): path = Service(driver_path) self.driver = webdriver.Firefox(service=path) self.mouse = ActionChains(self.driver) def find_element_xpath(self,xpath_path,key_word): self.driver.find_element(By.XPATH,xpath_path).send_keys(key_word) def element_webdriverwait(self,sec,element): driver_wait = WebDriverWait(self.driver,sec) driver_wait.until(lambda x: x.find_element_xpath(By.XPATH,element))
转载地址:https://blog.csdn.net/xiaoqiukuzi/article/details/129164641 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2023年05月25日 12时03分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
sqlserver 创建数据库用户限定用户访问的库以及ip
2019-09-15 17:46:45
使用sql server 链接服务器 访问 access的方法和步骤
2019-09-15 17:46:45
CentOS下Samba服务器的安装与配置步骤
2019-09-15 17:46:45
linux 下搭建maven私服
2019-09-15 17:46:44
linux下安装maven
2019-09-15 17:46:44
nginx 搭建图片服务器
2019-09-15 17:46:43
tomcat在window下开机自启
2019-09-15 17:46:42
tomcat 配置
2019-09-15 17:46:42
配置tomcat的初始化参数
2019-09-15 17:46:41
nginx反向代理时的超时时间设置
2019-09-15 17:46:41
windows2008服务器设置系统启动时程序自动运行
2019-09-15 17:46:40
配置 windows server FTP开启防火墙例外后然然无法访问的解决办法
2019-09-15 17:46:40
redis的常用命令
2019-09-15 17:46:39
linux下的redis安装 ,连接,测试
2019-09-15 17:46:39
sql server 进行数据的转移 清洗(sql server到mysql)
2019-09-15 17:46:38
sqlserver通过linkserver连接mysql进行数据的操作
2019-09-15 17:46:38
nginx的配置
2019-09-15 17:46:37
linux环境下安装nginx步骤
2019-09-15 17:46:37
mysql中Prepare、execute、deallocate的使用方法
2019-09-15 17:46:36
mysql 有表中使用动态的变量
2019-09-15 17:46:36