如何在Selenium 2.53.1中将我的Firefox浏览器设置为私有模式?

时间:2017-01-11 09:48:32

标签: java selenium firefox

关于selenium 2.53.0(我不想更新到3.0,因为我需要使用Firefox而Selenium没有为FF实施操作)。

我的问题是:我尝试在私人模式下打开FF(我不想保留缓存,因为我运行了很多FF实例)

为此,我使用FF配置文件并且我有证书,所以我接受它。

我的代码是:

        FirefoxDriverManager.getInstance().setup();

        capabilities.setBrowserName("firefox");
        capabilities.setVersion("46");

        ProfilesIni profile = new ProfilesIni();
        FirefoxProfile ffProfile = profile.getProfile("default");

        ffProfile.setPreference("browser.private.browsing.autostart", true); 
        ffProfile.setPreference("browser.privatebrowsing.autostart", true);

        //accept the certificate
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);
        capabilities.setCapability(FirefoxDriver.PROFILE, ffProfile);
        WebDriver webDriver = new FirefoxDriver();

我尝试了"browser.private.browsing.autostart""browser.privatebrowsing.autostart",因为当我在firefox中执行about:config时,我发现了这两个。

我没有收到任何错误,firefox运行我的测试但不是私下。你有什么主意吗? 我找到了这个post,但它没有答案。

2 个答案:

答案 0 :(得分:4)

在上面的代码示例中,您尝试两次来设置配置文件。尝试删除其中一个参数?只要

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

不适合您,有一种替代解决方案;所以你可以工作直到你搞清楚了。

在您要访问的页面中查找要打开私人窗口的任何元素。例如:

Driver.FindElement(By.ByXpath(//div[@id="loginButton"])).sendKeys(Keys.chord(Keys.CONTROL, Keys.SHIFT, "P"));

然后发送 Ctrl + Shift + p 给它..然后你就可以使用新的私有测试窗口。也许不是最好的解决方案,但是,这将是100%工作,只是在我的机器上测试,如果你没有做到这一点,请在下面评论。

祝你好运!

答案 1 :(得分:2)

FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);

目前正在使用FF v54和selenium 3.4.0