在使用 Selenium v3.11.x , GeckoDriver v0.20.0 和 Firefox Quantum v59.0.2时
,可以使用不同的选项来调用新的/现有的 Firefox配置文件
如果您希望在每次执行 测试时 使用 新的 Firefox配置文件 ,则可以使用以下代码块: __
System.setProperty("webdriver.gecko.driver", "C:\path\to\geckodriver.exe"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(new FirefoxProfile()); WebDriver driver = new FirefoxDriver(options); driver.get("https://www.google.com");如果您正在寻找使用一个 现有 的Firefox配置文件 在您每次运行 测试执行 首先你必须创建一个 Firefox的配置文件
手动下面的说明
在Windows上创建新的Firefox配置文件
。
现在,您有两种方法可以调用已创建的 Firefox配置文件 ,如下所示:
- 您可以使用 FirefoxOptions 类来调用现有的 Firefox配置文件, 并且可以使用以下代码块:
System.setProperty("webdriver.gecko.driver", "C:\path\to\geckodriver.exe"); ProfilesIni profile = new ProfilesIni(); FirefoxProfile testprofile = profile.getProfile("debanjan"); FirefoxOptions opt = new FirefoxOptions(); opt.setProfile(testprofile); WebDriver driver = new FirefoxDriver(opt); driver.get("https://www.google.com");- 您还可以使用 DesiredCapabilities 类设置现有的 Firefox配置文件 ,然后在 FirefoxOptions 实例中合并,并且可以使用以下代码块:
System.setProperty("webdriver.gecko.driver", "C:\path\to\geckodriver.exe"); ProfilesIni profile = new ProfilesIni(); FirefoxProfile testprofile = profile.getProfile("debanjan"); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, testprofile); FirefoxOptions opt = new FirefoxOptions(); opt.merge(dc); WebDriver driver = new FirefoxDriver(opt); driver.get("https://www.google.com");


