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

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

问题描述

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

硒:

driver.implicitly_wait(10)

Python:

导入时间时间.睡眠(10)

这两者有什么区别吗?

解决方案

time.sleep(secs)

<块引用>

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

您可以在How to在 python 中休眠 webdriver 几毫秒

<小时>

implicitly_wait(time_to_wait)

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

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

因此我们引入了ImplicitWait.通过引入 ImplicitWait驱动程序 将轮询 DOM 树 直到在配置的时间内找到该元素,然后在抛出 NoSuchElementException.到那时,您一直在寻找的一个或多个元素可能在 HTML DOM 中可用.在您的代码中,您已经将 ImplicitWait 设置为 10 秒的值,驱动程序 将轮询 HTML DOM 10 秒.

您可以在在 selenium 中使用隐式等待

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

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

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