我的应用程序使用 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 实例或询问用户是否希望关闭浏览器,但这并不理想。