python&硒:driver.implicitly_wait()和time.sleep()之间的区别 [英] Python & Selenium: Difference between driver.implicitly_wait() and time.sleep()

查看:413
本文介绍了python&硒:driver.implicitly_wait()和time.sleep()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道两者都用于等待指定的时间.

硒:

driver.implicitly_wait(10)

Python:

import time
time.sleep(10)

这两者之间有什么区别吗?

解决方案

time.sleep(secs)

time.sleep(secs) 在给定的秒数内暂停当前线程的执行.该参数可以是浮点数,以指示更精确的睡眠时间.实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep().另外,由于系统中其他活动的安排,暂停时间可能比请求的时间长任意数量.

您可以在如何找到详细讨论在python中休眠webdriver数毫秒


隐式等待(time_to_wait)

implicitly_wait(time_to_wait)用于指定 WebDriver 实例,即 driver 在搜索元素时应等待的时间它不会立即出现在 HTML DOM 内,试图查找一个或多个元素(如果不是立即可用)时的SECONDS术语.默认设置为 0 ,这意味着driver当找到用于查找一个或多个元素的指令时,搜索开始并且结果立即可用.

在这种情况下,在重新加载网页后,可能立即搜索到一个或多个元素.因此,您的自动化脚本可能会遇到以下任何异常:

因此,我们介绍 ImplicitWait .通过引入 ImplicitWait 驱动程序将轮询 DOM树 ,直到在配置的时间内找到该元素,然后在抛出在硒中使用隐式等待

Yes, I know both are used to wait for some specified time.

Selenium:

driver.implicitly_wait(10)

Python:

import time
time.sleep(10)

Is there any difference between these two?

解决方案

time.sleep(secs)

time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

You can find a detailed discussion in How to sleep webdriver in python for milliseconds


implicitly_wait(time_to_wait)

implicitly_wait(time_to_wait) is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of SECONDS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.

In this case, after a fresh loading of a webpage an element or elements may be / may not be found on an immediate search. So your Automation Script may be facing any of these exceptions:

Hence we introduce ImplicitWait. By introducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.

You can find a detailed discussion in Using implicit wait in selenium

这篇关于python&硒:driver.implicitly_wait()和time.sleep()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆