<i id='XbrW0'><tr id='XbrW0'><dt id='XbrW0'><q id='XbrW0'><span id='XbrW0'><b id='XbrW0'><form id='XbrW0'><ins id='XbrW0'></ins><ul id='XbrW0'></ul><sub id='XbrW0'></sub></form><legend id='XbrW0'></legend><bdo id='XbrW0'><pre id='XbrW0'><center id='XbrW0'></center></pre></bdo></b><th id='XbrW0'></th></span></q></dt></tr></i><div id='XbrW0'><tfoot id='XbrW0'></tfoot><dl id='XbrW0'><fieldset id='XbrW0'></fieldset></dl></div>
    • <bdo id='XbrW0'></bdo><ul id='XbrW0'></ul>

  1. <tfoot id='XbrW0'></tfoot>

      <small id='XbrW0'></small><noframes id='XbrW0'>

    1. <legend id='XbrW0'><style id='XbrW0'><dir id='XbrW0'><q id='XbrW0'></q></dir></style></legend>

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

      How to sleep Selenium WebDriver in Python for milliseconds(如何让 Python 中的 Selenium WebDriver 休眠几毫秒)
      <i id='C0V0h'><tr id='C0V0h'><dt id='C0V0h'><q id='C0V0h'><span id='C0V0h'><b id='C0V0h'><form id='C0V0h'><ins id='C0V0h'></ins><ul id='C0V0h'></ul><sub id='C0V0h'></sub></form><legend id='C0V0h'></legend><bdo id='C0V0h'><pre id='C0V0h'><center id='C0V0h'></center></pre></bdo></b><th id='C0V0h'></th></span></q></dt></tr></i><div id='C0V0h'><tfoot id='C0V0h'></tfoot><dl id='C0V0h'><fieldset id='C0V0h'></fieldset></dl></div>
      <tfoot id='C0V0h'></tfoot>

          <bdo id='C0V0h'></bdo><ul id='C0V0h'></ul>

          1. <small id='C0V0h'></small><noframes id='C0V0h'>

              • <legend id='C0V0h'><style id='C0V0h'><dir id='C0V0h'><q id='C0V0h'></q></dir></style></legend>
                  <tbody id='C0V0h'></tbody>
              • 本文介绍了如何让 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
                

                但是,在使用 SeleniumWebDriver 进行 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

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                env: python: No such file or directory(env: python: 没有这样的文件或目录)
                How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                      <tfoot id='wQoo1'></tfoot>
                      • <bdo id='wQoo1'></bdo><ul id='wQoo1'></ul>
                        1. <legend id='wQoo1'><style id='wQoo1'><dir id='wQoo1'><q id='wQoo1'></q></dir></style></legend>
                        2. <small id='wQoo1'></small><noframes id='wQoo1'>

                            <tbody id='wQoo1'></tbody>

                          <i id='wQoo1'><tr id='wQoo1'><dt id='wQoo1'><q id='wQoo1'><span id='wQoo1'><b id='wQoo1'><form id='wQoo1'><ins id='wQoo1'></ins><ul id='wQoo1'></ul><sub id='wQoo1'></sub></form><legend id='wQoo1'></legend><bdo id='wQoo1'><pre id='wQoo1'><center id='wQoo1'></center></pre></bdo></b><th id='wQoo1'></th></span></q></dt></tr></i><div id='wQoo1'><tfoot id='wQoo1'></tfoot><dl id='wQoo1'><fieldset id='wQoo1'></fieldset></dl></div>