Selenium Firefox驱动程序 - 由于“DirectoryNotFoundException”无法加载Firefox个人资料

4

我有最新版本的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:“找不到路径'C:\Users\XXX\AppData\Local\Temp\anonymous.5bbc89e65ae54c058b27b9027039414b.webdriver-profile.parentlock'的一部分。”
当您查看目录时,“anonymous.5bbc89e65ae54c058b27b9027039414b.webdriver-profile”文件夹不存在。
通过调用以下代码,我可以生成一个文件夹:
firefoxProfile.WriteToDisk();

然而,运行代码的最后一行之后,我仍然会得到相同的错误,只是在不同的“匿名”文件夹中。
通过启用“UseLegacyImplementation”可以解决这个问题,但这会引入其他问题,并且不是最佳选择。
查看周围,我没有在任何地方找到这条消息的参考资料,在Github上有一些相关内容,但与配置文件被忽略无关。
我曾经在较旧版本的库和Firefox上使用类似的代码,但出现了这个问题。是否有人对此有任何建议?

我注意到当你实例化一个新的FireFoxDriver对象时,你使用了一个FireFoxDriverService作为第一个参数。在这份文档中,我看到使用了GeckoDriverService:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxDriver.html。你尝试过使用GeckoDriverService吗? - RKelley
@RKelley 或许在Java中有,但我没有在c#库中看到,这是个好问题,“Selenium.WebDriver.GeckoDriver”可以通过NuGet获取,我认为它只是将EXE文件复制到bin目录下,检查packages下的“driver”文件夹可以证实我的猜想。NuGet包只是EXE文件。不确定在c#中是否可行。 - David Rogers
抱歉,我已经沉浸在Java世界太久了。 - RKelley
1个回答

2

我成功地复现了您的问题,但是采取了以下措施并解决了它。

最初的回答:

  1. I created a new profile from Firefox about:profiles -> New profile Name = TestUser

  2. Copied the location of this profile (Root Directory) and used it when creating the instance of FirefoxProfile

    var firefoxProfile = new FirefoxProfile(@"C:\Users\[user]\AppData\Roaming\Mozilla\Firefox\Profiles\67fkrqcg.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引起的,根据文档,该方法删除配置文件的锁定文件。 我怀疑您忘记创建配置文件和/或未指定正确的路径。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接