如何在python中休眠Webdriver毫秒

编程入门 行业动态 更新时间:2024-10-25 14:26:34
本文介绍了如何在python中休眠Webdriver毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在脚本中使用time库:

import time time.sleep(1)

它可以使我的Web驱动程序休眠1秒钟,但我需要使其休眠250毫秒.

解决方案

要将网络驱动程序的执行暂停毫秒,您可以按以下方式传递number of seconds或floating point number of seconds:

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

但是,在没有任何特定条件的情况下,使用 time.sleep(secs) 使用 Selenium 和 WebDriver 进行自动化时实现 违反了自动化 的目的,应不惜一切代价避免这样做.根据文档:

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

因此,按照讨论而不是time.sleep(sec),您应该使用 WebDriverWait() 与 expected_conditions() 来验证元素的状态,而三个被广泛使用的Expected_condition是如下:

已存在的元素

presence_of_element_located(定位符)的定义如下:

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(定位符)的定义如下:

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无法正常工作

I am using the time library in my script:

import time time.sleep(1)

It can sleep my webdriver for 1 second but I need to sleep it 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中休眠Webdriver毫秒

本文发布于:2023-10-30 22:28:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   python   Webdriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!