如何在 Python 中使 Selenium WebDriver 休眠几毫秒 [英] How to sleep Selenium WebDriver in Python for milliseconds

查看:101
本文介绍了如何在 Python 中使 Selenium WebDriver 休眠几毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在脚本中使用了 time 库:

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

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

解决方案

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

导入时间time.sleep(1) #睡眠1秒time.sleep(0.25) #sleep 250 毫秒

但是,在使用 SeleniumWebDriver 进行自动化 时,使用 time.sleep(secs) 没有任何特定条件来实现 违背了自动化的目的,应该不惜一切代价避免.根据文档:

<块引用>

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

<小时>

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

presence_of_element_located

presence_of_element_located(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)参数:locator - 用于查找元素,一旦找到就返回 WebElement描述:检查元素是否存在于页面的 DOM 上的期望.这并不一定意味着该元素是可见的或可交互的(即可点击的).

visibility_of_element_located

visibility_of_element_located(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)参数:locator - 用于查找元素,一旦它被定位并可见就返回 WebElement描述:检查元素是否存在于页面的 DOM 上并且可见的期望.可见性意味着元素不仅被显示,而且其高度和宽度都大于 0.

element_to_be_clickable

element_to_be_clickable(locator) 定义如下:

class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)参数:locator - 用于查找元素,一旦它可见、启用和可交互(即可点击),就会返回 WebElement.描述:检查元素是否可见、启用和可交互的期望,以便您可以单击它.

<小时>

参考

您可以在WebDriverWait 未按预期工作

I am using the time library in my script:

import time
time.sleep(1)

It can sleep my Selenium WebDriver for one second, but how is it possible for 250 milliseconds?

解决方案

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

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) 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.


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

presence_of_element_located(locator) is defined as follows :

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) is defined as follows :

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) is defined as follows :

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. 


Reference

You can find a detailed discussion in WebDriverWait not working as expected

这篇关于如何在 Python 中使 Selenium WebDriver 休眠几毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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