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

EdgeDriver(C#)无法保存会话数据问题求助

问题描述

我用以下代码启动Edge或Chrome浏览器:

int ProcessID = -1;
public enum BrowserType
{
    Chrome=1,
    Edge=2
}
public void OpenChrome(
       string Website,
       int TimeToWaitInMinutes,
       string FolderPathToStoreSession,
       out int ChromeProcessID)
{
    try
    {
        //chrome process id
        int ProcessID = -1;
        //time to wait until open chrome
        var TimeToWait = TimeSpan.FromSeconds(TimeToWaitInMinutes);

        if (browserValue == BrowserType.Chrome)
        {
            ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
            //hide dos screen
            cService.HideCommandPromptWindow = true;
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
            options.AddArguments("chrome.switches", "--disable-extensions");
            driver_Ref = new ChromeDriver(cService, options, TimeToWait);
            ProcessID = cService.ProcessId;
        }
        else
        {
            EdgeDriverService cService = EdgeDriverService.CreateDefaultService(".", "msedgedriver.exe");
            //hide dos screen
            cService.HideCommandPromptWindow = true;

            EdgeOptions options = new EdgeOptions();
            options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
            driver_Ref = new EdgeDriver(cService, options, TimeToWait);
            ProcessID = cService.ProcessId;
        }
        driver_Ref.Navigate().GoToUrl(Website);

        ChromeProcessID = ProcessID;
    }
    catch (Exception ex)
    {
        if (driver_Ref != null)
        {
            try
            {
                driver_Ref.Close();
                driver_Ref.Quit();
                driver_Ref.Dispose();
            }
            catch { }
        }

        driver_Ref = null;
        ChromeProcessID = -1;
        throw ex;
    }
}

使用的依赖及版本:

  • Microsoft.Edge.SeleniumTools 3.141.3
  • Selenium.Support 3.141.0
  • Selenium.WebDriver 3.141.0
  • Selenium.WebDriver.MSEdgeDriver 105.0.1343.27
  • Microsoft Edge Version 105.0.1343.42 (Official build) (64-bit)

Chrome浏览器可正常将登录信息等会话数据保存到指定目录,但Edge无报错却无法保存登录及浏览器数据,options.AddArgument("--user-data-dir=" + FolderPathToStoreSession)在Edge中无效。

解决方案

针对Edge浏览器的--user-data-dir参数无效问题,可按以下方式处理:

  1. 修正EdgeOptions配置
    基于Chromium的Edge需要明确启用Chromium模式,且--user-data-dir参数建议搭配--profile-directory使用,避免Edge默认生成临时目录。修改Edge分支的代码如下:

    else
    {
        EdgeDriverService cService = EdgeDriverService.CreateDefaultService(".", "msedgedriver.exe");
        cService.HideCommandPromptWindow = true;
    
        EdgeOptions options = new EdgeOptions();
        // 明确指定使用Chromium内核
        options.UseChromium = true;
        // 设置用户数据目录
        options.AddArgument($"--user-data-dir={FolderPathToStoreSession}");
        // 指定默认配置文件目录,确保会话数据写入正确位置
        options.AddArgument("--profile-directory=Default");
    
        driver_Ref = new EdgeDriver(cService, options, TimeToWait);
        ProcessID = cService.ProcessId;
    }
    
  2. 检查目录权限与存在性
    确认FolderPathToStoreSession指定的目录已创建,且程序拥有该目录的读写权限。如果目录不存在,手动创建后再测试。

  3. 清理Edge后台进程
    运行代码前关闭所有Edge浏览器进程(包括后台驻留的),避免已有进程占用用户数据目录导致新配置不生效。

  4. 核对驱动与浏览器版本
    当前浏览器版本为105.0.1343.42,驱动版本为105.0.1343.27,前三位版本号一致理论上兼容,但如果问题依旧,可尝试更新驱动至与浏览器完全匹配的版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:30:58