资讯专栏INFORMATION COLUMN

聊聊selenium的webdriver的超时参数

yanest / 440人阅读

摘要:序本文主要介绍下的的超时参数。超时参数相当于设置全局的等待,在定位元素时,对所有元素设置超时时间,超出了设置时间则抛出异常。超过这个时间则抛出异常。默认为,即永不超时。

本文主要介绍下selenium的webdriver的超时参数。

超时参数

selenium-api-2.53.1-sources.jar!/org/openqa/selenium/WebDriver.java

 /**
   * An interface for managing timeout behavior for WebDriver instances.
   */
  interface Timeouts {

    /**
     * Specifies the amount of time the driver should wait when searching for an element if it is
     * not immediately present.
     * 

* When searching for a single element, the driver should poll the page until the element has * been found, or this timeout expires before throwing a {@link NoSuchElementException}. When * searching for multiple elements, the driver should poll the page until at least one element * has been found or this timeout has expired. *

* Increasing the implicit wait timeout should be used judiciously as it will have an adverse * effect on test run time, especially when used with slower location strategies like XPath. * * @param time The amount of time to wait. * @param unit The unit of measure for {@code time}. * @return A self reference. */ Timeouts implicitlyWait(long time, TimeUnit unit); /** * Sets the amount of time to wait for an asynchronous script to finish execution before * throwing an error. If the timeout is negative, then the script will be allowed to run * indefinitely. * * @param time The timeout value. * @param unit The unit of time. * @return A self reference. * @see JavascriptExecutor#executeAsyncScript(String, Object...) */ Timeouts setScriptTimeout(long time, TimeUnit unit); /** * Sets the amount of time to wait for a page load to complete before throwing an error. * If the timeout is negative, page loads can be indefinite. * * @param time The timeout value. * @param unit The unit of time. * @return A Timeouts interface. */ Timeouts pageLoadTimeout(long time, TimeUnit unit); }

implicitlyWait

相当于设置全局的等待,在定位元素时,对所有元素设置超时时间,超出了设置时间则抛出异常。默认为0即不等待。

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
没有设置implicitlyWait,则driver.findElement()只会找一遍指定元素,找不到就马上抛异常

scriptTimeout

设置异步脚本执行的超时时间,超出则抛异常。

The amount of time, in milliseconds, this session should wait for asynchronous scripts to finish executing. If set to 0, then the timeout will not fire until the next event loop after the script is executed. This will give scripts that employ a 0-based setTimeout to finish.

pageLoadTimeout

用来设置页面完全加载的超时时间,完全加载即页面全部渲染。超过这个时间则抛出异常。默认为-1,即永不超时。

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

这里是否包含异步脚本都执行完成呢,一般onload是指外部资源加载完,而异步脚本执行完跟这个应该不是一回事。

doc

selenium webdriver(5)—超时设置

WebDriver: Advanced Usage

webdriver-timeouts

WebDriverWait等设置等待时间和超时时间

The default value of timeouts on selenium webdriver

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67831.html

相关文章

  • 【Python爬虫】4万字,详解selenium从入门到实战【错过再无】

    摘要:难在哪里根据上面的标签需要定位最后一行标签,以下列出了四种方式,定位的方式多样并不唯一,使用时根据情况进行解析即可。加入每日一练我们使用并指明标签内全部文本即可定位。 ...

    shiyang6017 评论0 收藏0
  • 爬虫 + 自动化利器 selenium 之自学成才篇(二)

    摘要:耗时代码运行到这句之后触发隐式等待,在轮询检查后仍然没有定位到元素,抛出异常。耗时值得一提的是,对于定位不到元素的时候,从耗时方面隐式等待和强制等待没什么区别。 ...

    AWang 评论0 收藏0
  • Python2+Selenium入门04-关于WebDriver

    摘要:和是中两个非常重要的类,使用它们提供的方法可以实现与浏览器和页面元素的各种交互操作。该方法在每个只需要调用一次等待时间单位为秒设置一个页面完全加载完成的超时等待时间等待时间单位为秒设置脚本执行的超时时间,应该在抛出错误之前等待时间单位为秒 WebDriver 和 WebElement 是 Selenium 中两个非常重要的类,使用它们提供的方法可以实现与浏览器和页面元素的各种交互操作。...

    vslam 评论0 收藏0
  • Python3网络爬虫实战---37、动态渲染页面抓取:Selenium

    摘要:不过动态渲染的页面不止这一种。再有淘宝这种页面,它即使是获取的数据,但是其接口含有很多加密参数,我们难以直接找出其规律,也很难直接分析来抓取。我们用一个实例来感受一下在这里们依然是先打开知乎页面,然后获取提问按钮这个节点,再将其 上一篇文章:Python3网络爬虫实战---36、分析Ajax爬取今日头条街拍美图下一篇文章:Python3网络爬虫实战---38、动态渲染页面抓取:Spla...

    zhjx922 评论0 收藏0

发表评论

0条评论

yanest

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<