【问题标题】:Selenium fails to open new firefox binary with profile when FF is already open当 FF 已打开时,Selenium 无法使用配置文件打开新的 firefox 二进制文件
【发布时间】:2013-09-17 04:50:43
【问题描述】:

我的应用使用 selenium 来自动化浏览器 - 不是为了测试应用,而是实际成为正在运行的应用的一部分。

我正在尝试在 selenium 中加载 FF 的配置文件,但是当我打开 FF 版本时它不起作用。我正在尝试用下面的方法来解决它。

        IEnumerable<int> pidsBefore = Process.GetProcessesByName("firefox").Select(p => p.Id);

        FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
        profile.SetPreference("browser.tabs.loadInBackground", false); // set preferences you need
        if (pidsBefore.Count() > 0)
        {
  //this next line is where I need to grab the currently open browser somehow - I know this wont work as it wont bring up the selenium webdriver plugin but ...
            driver = new FirefoxDriver((FirefoxBinary)Process.GetProcessById(pidsBefore.Last()), profile, TimeSpan.FromSeconds(30));
        }
        else
        {
            driver = new FirefoxDriver(new FirefoxBinary(), profile, TimeSpan.FromSeconds(30));
              //this also fails below

              // driver = new FirefoxDriver(profile); 
              // The process cannot access the file 'C:\Users\me\AppData\Roaming\Mozilla\Firefox\Profiles\4xcr92nu.default-1373036135509\parent.lock' because it is being used by another process.
        }
        IEnumerable<int> pidsAfter = Process.GetProcessesByName("firefox").Select(p => p.Id);
        IEnumerable<int> newFirefoxPids = pidsAfter.Except(pidsBefore);
        foreach (int pid in newFirefoxPids)
        {
            Program.newBrowserPid=pid;
   //I could kill all open instances here but I dont want to
        }
        driver.SwitchTo();

我可以关闭现有的 FF 实例或询问用户是否希望关闭浏览器,但这并不理想。

【问题讨论】:

  • 我发现这种方法存在固有缺陷。没有真正的方法可以检测浏览器实例是由 Selenium 启动还是由用户启动。你为什么要这样做,有什么特别的原因吗?当您收到该错误时,打开了什么?多少个浏览器实例?到那时 Selenium 打开了多少个?你开了几个?
  • 我根本没有打开浏览器。我启动 selenium 以使用用户配置文件打开浏览器,然后我再次使用用户配置文件手动打开另一个浏览器,然后启动 selenium 尝试使用当前浏览器并抱怨锁定。
  • 另一种情况。使用默认配置文件打开单个浏览器,我尝试启动 selenium 以使用默认配置文件并抱怨锁定,然后我终止正在运行的实例,重新尝试打开浏览器和配置文件并且它可以工作。我能够获取 PID,因此我可以将选项卡添加到当前浏览器而不是启动新浏览器,它工作正常,我可以使用 sendkeys 在浏览器上打开多个选项卡。然后我打开另一个浏览器手动启动它 - 然后尝试在第一个浏览器上使用 selemium 打开一个新选项卡,它抱怨 parent.lock。

标签: c# selenium webdriver selenium-webdriver


【解决方案1】:

创建默认配置文件的副本,然后将您的 selenium 指向此复制的配置文件是解决此问题的一种方法。这样您就不必杀死其他 FF 进程。

FirefoxProfile profile = new FirefoxProfile(@"C:\Users\YourName\AppData\Roaming\Mozilla\Firefox\Profiles\CopyOfProfile");
FirefoxDriver driver = new FirefoxDriver(profile);

如果您需要它与原始配置文件保持同步,您可以监控原始目录并将更改复制到副本,或者只运行定期重新复制的计划任务。根据您要自动化的内容,可能完全没有必要让它保持最新状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多