如何让 Python 中的 Selenium WebDriver 休眠几毫秒
问题描述
我在我的脚本中使用 time
库:
I am using the time
library in my script:
import time
time.sleep(1)
它可以让我的 Selenium WebDriver 睡一秒钟,但怎么可能睡 250 毫秒?
It can sleep my Selenium WebDriver for one second, but how is it possible for 250 milliseconds?
解决方案
要暂停webdriver的执行毫秒,你可以传递秒数
或浮点数秒数
如下:
To suspend the execution of the webdriver for milliseconds you can pass number of seconds
or floating point number of seconds
as follows:
import time
time.sleep(1) #sleep for 1 sec
time.sleep(0.25) #sleep for 250 milliseconds
但是,在使用 Selenium 和 WebDriver 进行 Automation 时,使用 time.sleep(secs)
没有任何特定条件来实现 违背了自动化的目的,应该不惜一切代价避免.根据文档:
However while using Selenium and WebDriver for Automation using time.sleep(secs)
without any specific condition to achieve defeats the purpose of Automation and should be avoided at any cost. As per the documentation:
time.sleep(secs)
将当前线程的执行暂停给定的秒数.该参数可以是一个浮点数,以指示更精确的睡眠时间.实际的挂起时间可能少于请求的时间,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep().此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长.
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.
<小时>
所以根据讨论而不是 time.sleep(sec)
你应该使用 WebDriverWait()
与 expected_conditions()
来验证一个元素的状态,三个广泛使用的expected_conditions如下:
So as per the discussion instead of time.sleep(sec)
you should use WebDriverWait()
in-conjunction with expected_conditions()
to validate an element's state and the three widely used expected_conditions are as follows:
presence_of_element_located(locator) 定义如下:
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
visibility_of_element_located
visibility_of_element_located(locator) 定义如下:
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
element_to_be_clickable
element_to_be_clickable(locator) 定义如下:
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
<小时>
参考
您可以在WebDriverWait not working as expected中找到详细讨论
Reference
You can find a detailed discussion in WebDriverWait not working as expected
相关文章