Selenium .set_script_timeout(n)有什么作用,它与driver.set_page_load_timeout(n)有何不同? [英] What does Selenium .set_script_timeout(n) do and how is it different from driver.set_page_load_timeout(n)?

查看:1240
本文介绍了Selenium .set_script_timeout(n)有什么作用,它与driver.set_page_load_timeout(n)有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python硒环境中,我不太了解driver.set_page_load_timeout(n) VS的确切区别. driver.set_script_timeout(n).两者似乎可以互换使用,以设置超时时间以通过driver.get(URL)加载URL,但有时也可以一起使用.

In context of python selenium, I don't quite understand the exact difference of driver.set_page_load_timeout(n) VS. driver.set_script_timeout(n). Both seem to be used interchangeable to set a timeout to load an URL via driver.get(URL), but sometimes also together.

场景1 :

driver.set_page_load_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)

场景2 :

driver.set_script_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)

这两种情况有何不同?哪些情况会在一种情况下触发超时,但另一种情况不会触发?

推荐答案

根据 Selenium-Python API文档 set_page_load_timeout(n)set_script_timeout(n)都是 timeout 方法用于配置 webdriver 实例以在程序执行期间遵守.

As per the Selenium-Python API Docs set_page_load_timeout(n) and set_script_timeout(n) both are timeout methods which are used to configure the webdriver instance to abide by during the program execution.

set_page_load_timeout(time_to_wait) 设置在引发错误之前等待页面加载完成的时间,其定义为:

set_page_load_timeout(time_to_wait) sets the amount of time to wait for a page load to complete before throwing an error and is defined as :

    def set_page_load_timeout(self, time_to_wait):
    """
    Set the amount of time to wait for a page load to complete
       before throwing an error.

    :Args:
     - time_to_wait: The amount of time to wait

    :Usage:
        driver.set_page_load_timeout(30)
    """
    try:
        self.execute(Command.SET_TIMEOUTS, {
        'pageLoad': int(float(time_to_wait) * 1000)})
    except WebDriverException:
        self.execute(Command.SET_TIMEOUTS, {
        'ms': float(time_to_wait) * 1000,
        'type': 'page load'})

您可以在 set_script_timeout(time_to_wait) 设置脚本在

set_script_timeout(time_to_wait) sets the amount of time that the script should wait during an execute_async_script (Javascript / AJAX Call) call before throwing an error and is defined as :

    def set_script_timeout(self, time_to_wait):
    """
    Set the amount of time that the script should wait during an
       execute_async_script call before throwing an error.

    :Args:
     - time_to_wait: The amount of time to wait (in seconds)

    :Usage:
        driver.set_script_timeout(30)
    """
    if self.w3c:
        self.execute(Command.SET_TIMEOUTS, {
        'script': int(float(time_to_wait) * 1000)})
    else:
        self.execute(Command.SET_SCRIPT_TIMEOUT, {
        'ms': float(time_to_wait) * 1000})

这篇关于Selenium .set_script_timeout(n)有什么作用,它与driver.set_page_load_timeout(n)有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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