在C#中的Selenium WebDriver中使用特定的Firefox配置文件 [英] Using a specific Firefox profile in Selenium WebDriver in C#

查看:404
本文介绍了在C#中的Selenium WebDriver中使用特定的Firefox配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用已经为Firefox 2和硒2设置的配置文件,但是没有有关C#的文档.我尝试过的代码如下:

I am trying to use a profile I already have set up for firefox with selenium 2 but there is no documentation for C#. The code I have attempted is as follows:

FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile(profileName);
driver = new FirefoxDriver(profile);

我看到的与Java相当的代码使用ProfilesIni而不是FirefoxProfileManager,但是在C#中不可用.以这种方式设置驱动程序时,使用的硒配置文件具有所有默认设置,而不是我要指向的配置文件中指定的设置.

Code that I have seen that is comparible in Java uses ProfilesIni instead of FirefoxProfileManager, but that is not available in C#. When setting up the driver in this way the selenium profile used has all the default settings instead of the settings specified in the profile I am trying to point to.

我不确定我使用的是正确的方法来检索配置文件,但是如果有人将Selenium 2与C#一起使用,则任何信息都将有所帮助.

I am not sure that I am using the correct methods to retrieve the profile, but if anyone has used Selenium 2 with C#, any information would be helpful.

推荐答案

我们使用这种方法来加载默认的firefox配置文件(您可以创建并加载自定义配置文件):

We use such method to load default firefox profile (you can create custom profile and load it):

private IWebDriver driver;  
string pathToCurrentUserProfiles = Environment.ExpandEnvironmentVariables("%APPDATA%") + @"\Mozilla\Firefox\Profiles"; // Path to profile
string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default", SearchOption.TopDirectoryOnly);
if (pathsToProfiles.Length != 0)
{
     FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
     profile.SetPreference("browser.tabs.loadInBackground", false); // set preferences you need
     driver = new FirefoxDriver(new FirefoxBinary(), profile, serverTimeout);
}
else
{
     driver = new FirefoxDriver();
}

这篇关于在C#中的Selenium WebDriver中使用特定的Firefox配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆