我在 Firefox 中有很多配置文件需要在 selenium 中打开,现在我想在隐私浏览模式下打开一个 Firefox 配置文件。如何在 Selenium 中使用 privatebrowsing 打开 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 配置文件?
最佳答案
你的语法不是Java,而是Python(我认为)。
ffprofile.setPreference("browser.privatebrowsing.autostart", true);
请注意 setPreference
和 true
的区别。
对于第二个问题,您可以指定所需版本的路径
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");
关于java - 在 Selenium 中使用 privatebrowsing 打开 Firefox 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34996182/