c# - 在 C# 中的 Selenium WebDriver 中使用特定的 Firefox 配置文件

标签 c# firefox selenium webdriver

我正在尝试使用我已经为带有 selenium 2 的 firefox 设置的配置文件,但没有用于 C# 的文档。我尝试的代码如下:

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

我看到在 Java 中可比较的代码使用 ProfilesIni 而不是 FirefoxProfileManager,但在 C# 中不可用。以这种方式设置驱动程序时,使用的 selenium 配置文件具有所有默认设置,而不是我试图指向的配置文件中指定的设置。

我不确定我是否使用了正确的方法来检索配置文件,但如果有人使用过带有 C# 的 Selenium 2,任何信息都会有所帮助。

最佳答案

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

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# - 在 C# 中的 Selenium WebDriver 中使用特定的 Firefox 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7615193/

相关文章:

c# - 如何使用 QBFC13 和 C# 获取一个月的所有发票并将其全部保存为 PDF?

css - 字体速记在 Firefox 中不起作用

javascript - Canvas 上的文本位置在 FireFox 和 Chrome 中不同

firefox - 对于 JNLP,我如何再次从此发布者对话框中看到 "Do you want to run this application"

python - 如何打包和分发具有需要安装驱动程序的依赖项的 Python 项目?

python - Selenium:查找相邻元素

c# - 为值对象序列化自定义 NewtonSoft.Json

c# - 如何在类之间传递对象

c# - 如何持久化动态控件 (c#)

python - selenium.common.exceptions.ElementNotVisibleException : Message: element not interactable using Selenium