gpt4 book ai didi

python - 如何让 Python 中的 Selenium WebDriver 休眠几毫秒

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:46 25 4
gpt4 key购买 nike

我在我的脚本中使用了 time 库:

import time
time.sleep(1)

它可以让我的 Selenium WebDriver 休眠一秒钟,但怎么可能休眠 250 毫秒呢?

最佳答案

暂停执行 webdriver 的毫秒数,您可以传递秒数浮点秒数,如下所示:

import time
time.sleep(1) #sleep for 1 sec
time.sleep(0.25) #sleep for 250 milliseconds

然而,在使用 SeleniumWebDriver 进行自动化时,使用 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.


因此根据讨论而不是 time.sleep(sec) 你应该使用 WebDriverWait()结合expected_conditions()验证元素的状态和三个广泛使用的 expected_conditions 如下:

存在_of_element_located

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 中找到详细的讨论

关于python - 如何让 Python 中的 Selenium WebDriver 休眠几毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52603847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com