Selenium Firefox 驱动程序 - 由于 "DirectoryNotFoundException" 无法加载 FirefoxProfile

Selenium Firefox Driver - Unable to Load FirefoxProfile Due to "DirectoryNotFoundException"

我有最新版本的 Firefox(62.0 32 位)、Selenium(3.14.0.0) 和 gecko 驱动程序(0.22.0 32 位)。我的代码如下:

var firefoxProfile = new FirefoxProfile("XXX");
FirefoxDriverService service = 
    FirefoxDriverService.CreateDefaultService("XXX", "geckodriver.exe");
service.FirefoxBinaryPath = "XXX";
driver = new FirefoxDriver(service, new FirefoxOptions { 
        BrowserExecutableLocation = "XXX", 
        Profile = firefoxProfile, 
        UseLegacyImplementation = false }, 
    new TimeSpan(0, 1, 30));

但是最后一行由于以下错误而失败:

System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\Users\XXX\AppData\Local\Temp\anonymous.5bbc89e65ae54c058b27b9027039414b.webdriver-profile.parentlock'.'

当您查看目录时,"anonymous.5bbc89e65ae54c058b27b9027039414b.webdriver-profile" 文件夹不存在。

我可以通过调用以下代码生成一个文件夹:

firefoxProfile.WriteToDisk();

但是我仍然会遇到同样的错误,只是在我的代码的最后一行 运行 之后有一个不同的 "anonymous" 文件夹。

我可以通过启用 "UseLegacyImplementation" 来解决这个问题,但这会引入其他问题并且不是最佳选择。

环顾四周,我没有看到任何地方引用此消息,Github 上引用了一些内容,但它指的是配置文件被忽略,而不是错误。

我在旧版本的库和 firefox 上有类似的代码,出于某种原因,当我尝试在不同的机器上实现所有最新版本时,我遇到了这个问题。有人对此有任何意见吗?

我成功地重现了你的问题,但做了以下并摆脱了它。

  1. 我从 Firefox 创建了一个新配置文件 about:profiles -> 新配置文件 Name = TestUser

  2. 复制了此配置文件的位置(根目录)并在创建 FirefoxProfile

    的实例时使用它
    var firefoxProfile = new FirefoxProfile(@"C:\Users\[user]\AppData\Roaming\Mozilla\Firefox\Profilesfkrqcg.TestUser");
    FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\geckodriver-v0.22.0-win32", "geckodriver.exe");
    service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";
    var driver = new FirefoxDriver(service, new FirefoxOptions
        {
            BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe",
            Profile = firefoxProfile,
            UseLegacyImplementation = false
        },
        new TimeSpan(0, 1, 30));
    

由于方法 DeleteLockFiles 调用引发错误,该方法根据 documentation 删除配置文件的锁定文件。

我怀疑您忘记创建配置文件and/or 没有指定正确的路径。