一尘不染

Selenium .set_script_timeout(n)有什么作用,它与driver.set_page_load_timeout(n)有何不同?

selenium

对于pythonselenium,我不太了解driver.set_page_load_timeout(n)VS
的确切区别。driver.set_script_timeout(n)。两者似乎可以互换使用,以设置超时时间以通过加载URL
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)

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


阅读 1192

收藏
2020-06-26

共1个答案

一尘不染

根据 Selenium-Python API Docs
set_page_load_timeout(n)set_script_timeout(n)两者都是 超时 方法,用于将 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})
2020-06-26