Selenium .set_script_timeout(n)做了什么以及它与driver.set_page_load_timeout(n)有什么不同?

时间:2018-02-23 04:42:47

标签: python selenium webdriver pageloadtimeout setscripttimeout

在python selenium的背景下,我不太了解driver.set_page_load_timeout(n) VS的确切差异。 driver.set_script_timeout(n)。两者似乎都可以互换使用来设置超时以通过driver.get(URL)加载网址,但有时也会一起加载。

场景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)

两种情况有何不同?哪种情况会在一种情况下触发超时而在另一种情况下不会触发?

1 个答案:

答案 0 :(得分:3)

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

set_page_load_timeout(TIME_TO_WAIT)

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

    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_page_load_timeout

的详细讨论

set_script_timeout(TIME_TO_WAIT)

set_script_timeout(time_to_wait)设置脚本在投出错误之前在execute_async_script Javascript / AJAX调用)调用期间应等待的时间并定义为:

    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})