python显示等待和隐式等待_selenium元素等待强制等待、显式等待、隐式等待详解...

1、强制等待

之前的脚本selenium打开网页后会强制sleep几秒然后退出,如果代码层面出现了quit(),有时候网页未加载完完浏览器就退出了。所以才要强制等待!sleep就是强制等待。

2、显式等待

每个网页加载时间是不同的,给每个网页sleep一样的时间也不科学,所以有一种显式等待的方法,人为设定最大时长,人为指定将网页中某个元素出现作为条件,直到该元素出现代码才往下执行,如果超时则报异常。

显式等待官方文档

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.

The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait.

There are some convenience methods provided that help you write code that will wait only as long as required.

WebDriverWait in combination with ExpectedCondition is one way this can be accomplished

3、隐式等待

此外,还有一种隐式等待的方法(也叫智能等待),人为指定一个等待时间,但是它并不针对页面上的某一固定元素进行等待,而是脚本只要有元素定位操作时就会进行判断如果元素可以定位到则往下执行,如果定位不到,它将以轮询的方式不断地判断元素是否被定位到了。一单超出设置的时长还没有定位到元素,则抛出异常。(隐式等待中浏览器会在设定的时间不断的刷新页面去寻找元素)。

隐式等待官方文档

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when

trying to find any element (or elements) not immediately available. The default setting is 0.

Once set, the implicit wait is set for the life of the WebDriver object.

下一篇用代码感受下强制等待、显示等待、隐式等待的区别。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值