- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在我的脚本中使用了 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
然而,在使用 Selenium 和 WebDriver 进行自动化时,使用 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 如下:
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(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(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/
我是一名优秀的程序员,十分优秀!