java - 在 Selenium 中使用 privatebrowsing 打开 Firefox 配置文件

标签 java maven firefox selenium selenium-webdriver

我在 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);

请注意 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");

关于java - 在 Selenium 中使用 privatebrowsing 打开 Firefox 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34996182/

相关文章:

javascript - Firefox 糟糕的渲染

firefox - SVG 模糊在 Firefox 中非常不稳定

firefox - 无法使用 firefox 登录到 stackexchange

java - 我试图在每次用户输入 "Yes"时添加 25,然后打印结果,但在将它们相加时始终缺少第一个响应

java - 将异常传输到 UI 的最佳方式?

maven - 在Kubernetes上部署Lagom微服务

spring - Spring Tomcat 应用程序中的 NoSuchMethodError

JAX-RS 和 Maven 的 java.lang.ClassNotFoundException

java - 如何在java中的for循环中将计算保存在同一变量中

java - @MatrixVariable Spring 3.2 返回 Null