在 selenium 中使用隐式等待 [英] Using implicit wait in selenium

查看:27
本文介绍了在 selenium 中使用隐式等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者.我理解等待基本上是做什么的,但我对互联网上的不同教程如何放置和解释它感到困惑.例如,在下面的代码中,它被放置在加载 URL 之前.那么,是只等待加载 URL 还是等待查找元素或两者兼而有之?如果我在 try 块中使用一次隐式等待,它是否适用于我在代码中执行的每个元素搜索,这是真的吗?

I am a beginner. I understand what waits basically does but I am confused over how different tutorials over the internet place it and explain it. For example, in the below code it is placed before loading the URL. So, is it only to wait for the URL to be loaded or for finding the element or both? Is is true that if I use an implicit wait once in my try block, it will be applicable for every element search I am performing in my code?

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

推荐答案

ImplicitWait

ImplicitWait 根据 Java Docs 是指定 WebDriver 实例,即 driver 在搜索元素时应等待的时间量,如果它没有立即出现在HTML DOMNANOSECONDS, 毫秒毫秒分钟小时当试图找到一个或多个元素时,如果它们不是立即可用的.默认设置为0,这意味着驱动程序当找到一个或多个元素的指令时,搜索开始并且结果立即可用.

ImplicitWait

ImplicitWait as per the Java Docs is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS or DAYS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.

在这种情况下,在重新加载网页后,一个或多个元素可能会/可能不会在立即搜索中找到.因此,您的 自动化框架可能会面临这些例外:

In this case, after a fresh loading of a Webpage an element or elements may be / may not be found on an immediate search. So your Automation Framework may be facing any of these exceptions:

因此我们引入 ImplicitWait.通过诱导 ImplicitWait驱动程序 将轮询 DOM 树 直到在配置的时间内找到元素,然后再抛出 NoSuchElementException.到那时,您一直在寻找的一个或多个元素可能在 HTML DOM 中可用.在您的代码中,您已经将 ImplicitWait 设置为 10 秒的值,驱动程序 将轮询 HTML DOM 10 秒.

Hence we introduce ImplicitWait. By inducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.

  • Python:

driver.implicitly_wait(10)

  • Java:

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

  • DotNet:

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
    

  • 最后,一旦你设置了 ImplicitWaitWebDriver 实例,即 驱动程序 能够携带此配置直到其生命周期.但是,如果您需要将 WebDriver 实例(即 driver 的粗略时间更改为 wait,则可以重新配置如下:

    Finally, once you set the ImplicitWait, the WebDriver instance i.e. the driver is able to carry this configuration till its lifetime. But if you need to change the coarse of time for the WebDriver instance i.e. the driver to wait then you can reconfigure it as follows:

    • Python:

    driver.implicitly_wait(5)
    

  • Java:

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    

  • DotNet:

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    

  • 如果您想在任何时候使 ImplicitWait 无效,您可以按如下方式重新配置它:

    If at any point of time you want to nullify the ImplicitWait you can reconfigure it as follows:

    • Python:

    driver.implicitly_wait(0)
    

  • Java:

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    

  • DotNet:

    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
    

    • ...等待 URL... :不,ImplicitWait 对页面加载没有影响.
    • ...用于查找元素...:是的,ImplicitWait 将定义 WebDriver 实例等待查找的粗略时间出元素或元素.
    • ...隐式等待一次... :是的,您只需要配置一次ImplicitWait,它适用于整个WebDriver 实例.
    • ...每个元素搜索...:是的,适用于任何时候调用 findElement()findElements().
    • ...Wait for the URL... : No, ImplicitWait have no effect on page loading.
    • ...For finding the element... : Yes, ImplicitWait will define the coarse of time the WebDriver instance will wait looking out for the element or elements.
    • ...Implicit wait once... : Yes, you need to configure ImplicitWait only once and it is applicable throughout the lifetime of the WebDriver instance.
    • ...Every element search... : Yes, applicable when ever findElement() or findElements() is invoked.

    这篇关于在 selenium 中使用隐式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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