在Selenium中使用私有浏览打开Firefox配置文件

时间:2016-01-25 15:26:15

标签: java maven firefox selenium selenium-webdriver

我在Firefox中有很多配置文件在selenium中打开,现在我想在私人浏览模式下打开Firefox配置文件。如何在Selenium中使用私有浏览打开Firefox配置文件?这是我的代码

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("1");
ffprofile.set_preference("browser.privatebrowsing.autostart", True);
WebDriver driver = new FirefoxDriver(ffprofile);
driver.get("http://proxy.cm/");
try {Thread.sleep(13000);} catch (InterruptedException e) {e.printStackTrace();}

此行显示错误

ffprofile.set_preference("browser.privatebrowsing.autostart", True); 

以上行显示错误“True无法解析为变量”。

我面临的另一个问题是Selenium没有更新Firefox 个人资料,我的意思是我安装了一些新的扩展,我也改变了一些 在firefox配置文件中设置,但Selenium始终使用旧设置,扩展名打开Firefox。 如何强制Selenium使用更新的新设置和扩展来打开firefox配置文件?

1 个答案:

答案 0 :(得分:3)

你的语法不是Java,而是Python(我认为)。

ffprofile.setPreference("browser.privatebrowsing.autostart", true);

请注意setPreferencetrue中的差异。

对于第二个问题,您可以指定所需版本的路径

FirefoxBinary binary = new FirefoxBinary(new File("path_to_firefox"));
FirefoxProfile ffprofile = profile.getProfile("1");
ffprofile.setPreference("browser.privatebrowsing.autostart", true);
WebDriver driver = new FirefoxDriver(binary, ffprofile);

另一种选择是在系统属性

中定义Firefox的路径
System.setProperty("webdriver.firefox.bin", "/Applications/Firefox-2.app/Contents/MacOS/firefox-bin");
相关问题