Selenium中的.set_script_timeout(n)是什么?它与driver.set_page_load_timeout(n)有何不同?

5
在Python Selenium的上下文中,我不太理解driver.set_page_load_timeout(n)driver.set_script_timeout(n)之间的确切区别。两者似乎可以交替使用,用于设置通过driver.get(URL)加载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个回答

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

set_page_load_timeout(time_to_wait)

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

这基本上意味着,如果没有执行 execute_async_script 调用,set_script_timeout 就不起作用了? - sudonym
1
是的,这就是它的实现方式。 - undetected Selenium
抱歉延迟了,数据库。 - sudonym

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接