且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Selenium WebDriver在Firefox中创建配置文件

更新时间:2022-03-12 22:42:29

您声明的方法调用只是从给定的配置文件信息目录中创建一个Java配置文件对象,然后通过WebDriver实例将其传递给Firefox.

The method call you stated simply creates a java profile object from the given directory of profile information which is then passed to Firefox via the WebDriver instance.

为了使Firefox能够持久保留驱动程序并使它可从配置文件管理器中使用,您需要在我的(Windows 7)计算机上编辑文件profile.ini,该文件位于:

In order to get Firefox to persist your driver and make it available from profile manager, you need to edit the file profiles.ini, on my (Windows 7) machine this was in:

%APPDATA%\ Roaming \ Mozilla \ Firefox

%APPDATA%\Roaming\Mozilla\Firefox

此文件夹中的Profiles目录包含现有Firefox配置文件的存储,当您要将现有配置文件用作新配置文件的模板时,可以很方便地复制它们.

The Profiles directory within this folder contains the stores of existing Firefox profiles, which are quite handy to copy when you want to use an existing profile as the template for a new one.

根据您的操作系统,您的行驶里程可能会有所不同,但是我相信您可以通过快速搜索找到它.以您的示例为例,然后将以下内容添加到该文件中(其中标题中的N是下一个未使用的配置文件编号):

Your mileage may vary depending on your OS, but I'm sure you can find it with a quick search. Using your example, you'd then add the following to this file (where N in the header is the next unused profile number):

[ProfileN]
Name=selenium
IsRelative=0
Path=D:\Selenium

这将导致Firefox配置文件管理器加载配置文件,然后允许您使用此配置文件手动启动Firefox进行配置或测试,这是我想做的.

This will cause Firefox Profile Manager to load the profile and will allow you to then launch Firefox manually with this profile to configure or test it, which is what I presume you want to do.

以这种方式创建命名配置文件后,可以将其分配给Selenium中的驱动程序,如下所示:

Once you have created a named profile in this way, you can assign it to your driver in Selenium like this:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);

其中"selenium"与profiles.ini文件中的名称"属性相同.

Where "selenium" is the same as the Name property in the profiles.ini file.