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

标签 python selenium webdriver pageloadtimeout

在 python selenium 的上下文中,我不太了解 driver.set_page_load_timeout(n) VS 的确切区别。 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)

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

最佳答案

根据 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)设置在抛出错误之前等待页面加载完成的时间量,定义为:

    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 Call) 在抛出错误之前调用并定义为:

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

关于python - Selenium .set_script_timeout(n) 有什么作用,它与 driver.set_page_load_timeout(n) 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48941260/

相关文章:

python - 使用工作 numpy : `ImportError: cannot import name scalarmath` 创建 conda env 时遇到问题

python - 从不同线程修改 Python 字典

java - Selenium Webdriver (Java) : What are the benefits (if any) of using an objectmap. 属性文件而不是页面对象类?

javascript - Sauce Labs 无法执行 javascript

python - 如何在 Django 中为 Chatterbot 指定自定义语料库路径?

python - 导入错误 : No module named 'gdbm' occuring while using source ~/. bashrc

javascript - 相同的选择器在常规浏览器和 Selenium 中给出不同的结果

python - 两个独立路径的 XPath?

python - 在 Pyramid + sqlalchemy 应用程序的(基于 Selenium 的)功能测试中访问测试数据库

java - 检查链接的 URL 状态代码时,无法将 HttpResponseCode 错误解析为类型