You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SeleniumBase+UC Driver如何实现脚本超30秒运行

解决SeleniumBase异步JS脚本超时问题

要让异步JS脚本运行超过30秒并成功获取结果,直接修改sb.driver.set_script_timeout可能不生效——因为SeleniumBase的execute_async_script方法有独立的超时控制逻辑,得用它提供的参数来配置:

方法一:执行脚本时显式指定超时

在调用execute_async_script时,通过timeout参数设置超时时间(单位:秒),覆盖默认的30秒限制:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://example.com")

    print("Running long JS…")

    # 显式设置超时为120秒,适配60秒的异步操作
    result = sb.execute_async_script("""
        const done = arguments[arguments.length - 1];
        setTimeout(() => { done("JS FINISHED!"); }, 60000);
    """, timeout=120)

    print("RESULT:", result)
    sb.sleep(5)

方法二:全局设置脚本超时

如果需要所有脚本执行都使用统一的超时时间,可以在初始化SB实例时直接配置script_timeout参数:

from seleniumbase import SB

# 全局设置脚本超时为120秒
with SB(uc=True, script_timeout=120) as sb:
    sb.open("https://example.com")

    print("Running long JS…")

    result = sb.execute_async_script("""
        const done = arguments[arguments.length - 1];
        setTimeout(() => { done("JS FINISHED!"); }, 60000);
    """)

    print("RESULT:", result)
    sb.sleep(5)

原方法失效的原因

SeleniumBase对底层WebDriver做了封装,execute_async_script会优先使用自身的超时参数,而非直接读取driver的script_timeout设置,所以必须通过上述两种方式配置才能生效。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 21:42:09