【问题标题】:Can't use existing Firefox profile in Selenium WebDriver using C#无法使用 C# 在 Selenium WebDriver 中使用现有的 Firefox 配置文件
【发布时间】:2018-03-22 01:04:33
【问题描述】:

我需要为 Firefox 使用共享配置文件,该配置文件在退出时不会被删除。似乎这可以使用FirefoxProfileFirefoxOptions 来完成。但它们似乎都不起作用:启动 geckodriver 时,它使用这样的临时配置文件

1507646897935 mozrunner::runner INFO 运行命令: "C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette" “-轮廓” "C:\Users\\AppData\Local\Temp\rust_mozprofile.uzI9KAmLQ1zP"

调试的时候发现profile的ProfileDirectory属性一直为null。

var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("Test");
var driver = new FirefoxDriver(profile);

配置文件 Test 是使用 firefox -pbefore 手动创建的。我也尝试过这样使用它的位置:

var profile = new FirefoxProfile(@"C:\Users\<MyUsername>\TestProfile", deleteSourceOnClean: false);

但同样的问题,无法弄清楚为什么这不起作用。

用过的软件

  • geckodriver 0.19.0
  • Selenium.Firefox.WebDriver 2.0.0 (NuGet)
  • Selenium.WebDriver 3.6.0 (NuGet)
  • ASP.NET Core 2.0

【问题讨论】:

  • 嗨,@Lion 你解决问题了吗?我有完全相同的问题,但尚未解决。谢谢!

标签: c# selenium firefox asp.net-core asp.net-core-2.0


【解决方案1】:

通过将我的配置文件的路径作为常规 CLI 参数传递给 Chrome 解决了这个问题:

var options = new ChromeOptions();
options.AddArgument(@"--user-data-dir=C:\Users\<MyUsername>\TestProfile");
var driver = new ChromeDriver(options);

应该也适用于 Firefox。但我需要切换到 Chrome,直到 FF 驱动程序中的另一个错误得到修复。这根本不是完全干净的解决方案,但它可以作为一种解决方法,直到找到更好的解决方案。

【讨论】:

    【解决方案2】:

    在 Firefox 中,我需要保留所有 cookie、历史记录、缓存等,但没有任何效果,因为显然 selenium 不是为了跨会话保存这些内容而构建的。

    由于没有针对 Firefox 的解决方案,这就是我破解它的方法

    1. 阅读 firefox.exe 命令行以了解 个人资料临时目录。
    2. 手动关闭浏览器,以免删除临时配置文件
    3. 移动临时配置文件并保留所有数据

    代码如下:

    IWebDriver _driver;
    var service = FirefoxDriverService.CreateDefaultService();
    
    //Start webdriver
    _driver = new FirefoxDriver(service, options);
    
    
    //get the webdriver commandline so we can get the path of the ff profile temp dir so we can save it later
    var proc = service.ProcessId;
    string cmdline = GetCommandLine(proc);
    string profilePath = cmdline.Substring(cmdline.IndexOf(" -profile ") + 10);
    
    
    
    
    
    //Do stuff with your browser
    
    
    
    
    //In order to move the temp profile dir, we have to 'manually' close the browser window.
    //There is no other way because the temp profile gets deleted if you use _driver.close()
    var ffprocess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle == $"{title} - Mozilla Firefox");
    ffprocess.CloseMainWindow();
    
    //Delete the old profile data so we can get the updated data.
    Directory.Delete(sProfile, true);
    
    //store the temp profile data
    Directory.Move(profilePath, sProfile);
    //this must be deleted, othervise the webdriver won't start next time
    File.Delete(sProfile + @"\user.js");
    
    
    
    
    string GetCommandLine(int process)
    {
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE CommandLine Like \"% -profile %\" AND ParentProcessID = {process}"))
        using (ManagementObjectCollection objects = searcher.Get())
        {
            return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
        }
    
    }
    

    【讨论】:

    • objects.Cast().SingleOrDefault() 给出错误提示找不到演员
    • @SarveshwarPM 将其设置为您要存储配置文件的任何位置
    • @SarveshwarPM firefox 启动了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2015-10-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多