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

Selenium ChromeDriver运行数小时后频繁报错求助

针对Selenium长时间运行后连接异常的解决方案

问题背景

基于C# .NET 4.6、Chrome 108和对应Selenium/ChromeDriver开发的自动化机器人,在长时间循环执行(1-2小时后)频繁抛出三类异常:

disconnected: Unable to receive message from renderer
(failed to check if window was closed: disconnected: Unable to receive message from renderer)
(Session info: headless chrome=108.0.5359.125)

An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL localhostURL The exception message was: An error occurred while sending the request.

The HTTP request to the remote WebDriver server for URL localhostURL timed out after 60 seconds.

本地与Docker环境均存在该问题,现有ChromeOptions参数优化无效,错误均在driver.Navigate().GoToUrl(PageUrl)行触发。


具体解决思路与建议

1. 定期重建WebDriver实例

长时间复用单个Driver会话易引发内存泄漏、渲染进程崩溃或连接失效,建议设置循环阈值,每执行N次任务后销毁当前Driver并重新初始化:

  • 可根据任务耗时自定义循环阈值(示例中设为20次)
  • 销毁时务必调用Quit()和Dispose()释放资源,避免残留进程占用内存

2. 优化ChromeOptions参数

补充更多提升稳定性的参数,同时修正原有参数的潜在问题:

  • 移除user-agent参数中的单引号,避免Chrome解析异常
  • 添加--disable-extensions/--disable-plugins禁用扩展与插件,减少冲突
  • 添加--incognito开启无痕模式,避免缓存、会话数据积累
  • 添加--window-size=1920,1080指定headless模式窗口尺寸,避免页面渲染异常
  • 添加--disable-background-networking禁用后台网络请求,降低资源消耗

3. 调整超时设置

默认60秒超时易触发请求超时异常,可延长页面加载与脚本执行超时时间:

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(60);

4. 针对性捕获异常并重建Driver

当捕获到连接断开、超时类WebDriver异常时,直接销毁当前Driver,避免后续循环复用失效会话:

  • 在catch块中判断异常信息,匹配关键词后标记Driver需重建

5. 监控资源占用

  • 本地环境:定期查看Chrome进程内存占用,若持续攀升需优化Driver销毁逻辑
  • Docker环境:为容器设置合理内存限制,避免OOM导致Chrome进程崩溃;同时可配置定期清理Chrome临时文件

6. 严格匹配Chrome与ChromeDriver版本

确保ChromeDriver版本与Chrome 108.0.5359.125完全一致,版本不匹配是引发连接异常的常见原因


优化后的代码示例

private void Func()
{
    var status = Status.Failed;
    ChromeDriver driver = null;
    int loopCount = 0;
    const int maxLoopsPerDriver = 20; // 自定义每N次循环重启Driver

    while (status == Status.Failed)
    {
        // 按需重建Driver实例
        if (driver == null || loopCount >= maxLoopsPerDriver)
        {
            // 清理旧实例资源
            driver?.Quit();
            driver?.Dispose();
            
            var options = new ChromeOptions();
            options.AddArgument("--headless");
            // 修正user-agent引号问题
            options.AddArgument("--user-agent=Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166");
            options.AddArgument("--no-sandbox");
            options.AddArgument("--disable-gpu");
            options.AddArgument("--disable-dev-shm-usage");
            options.AddArgument("--disable-extensions");
            options.AddArgument("--disable-plugins");
            options.AddArgument("--incognito");
            options.AddArgument("--window-size=1920,1080");
            options.AddArgument("--disable-background-networking");

            driver = new ChromeDriver(@"path/to/chromedrive.exe", options);
            // 延长超时时间
            driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
            driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(60);
            
            loopCount = 0;
        }

        try
        {
            driver.Navigate().GoToUrl(PageUrl);
            status = Func2(driver);
            loopCount++;
        }
        catch (WebDriverException ex)
        {
            WriteException(ex);
            // 遇到连接异常时强制重建Driver
            if (ex.Message.Contains("disconnected") || ex.Message.Contains("timed out"))
            {
                driver?.Quit();
                driver?.Dispose();
                driver = null;
            }
            status = Status.Failed;
        }
        catch (Exception ex)
        {
            WriteException(ex);
            status = Status.Failed;
            loopCount++;
        }
    }

    // 最终清理资源
    driver?.Quit();
    driver?.Dispose();
}

内容的提问来源于stack exchange,提问作者O. H

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 12:45:37