14

我想启动浏览器(FF,CHROME)来测试禁用cookies,我试过这个:

           service =
                    new ChromeDriverService.Builder()
                            .usingDriverExecutable(new File("src/test/resources/chromedriver"))
                            .usingAnyFreePort().build();
            try {
                service.start();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability("disable-restore-session-state", true);
            driver = new ChromeDriver(service, capabilities);

但这不起作用...

4

6 回答 6

11

我刚刚得到了 Firefox 的解决方案:

FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);

对于 Chrome(阻止所有 cookie)

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.cookies", 2);

对于 Chrome(仅阻止第三方 cookie)

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.cookies", 1);
options.AddUserProfilePreference("profile.cookie_controls_mode", 1);
于 2013-08-07T15:14:13.457 回答
4

您可以按如下方式禁用 Chrome cookie:

Map prefs = new HashMap();
prefs.put("profile.default_content_settings.cookies", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
于 2013-12-23T03:43:58.333 回答
2

您可以使用以下代码片段来禁用 Chrome 和 Firefox 浏览器中的 cookie。如果您希望启用 cookie,只需删除该功能即可。

Safari 不支持实现此目的的任何功能。

对于铬:

DesiredCapabilities caps = new DesiredCapabilities();

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> profile = new HashMap<String, Object>();
Map<String, Object> contentSettings = new HashMap<String, Object>();

contentSettings.put("cookies",2);
profile.put("managed_default_content_settings",contentSettings);
prefs.put("profile",profile);
options.setExperimentalOption("prefs",prefs);
caps.setCapability(ChromeOptions.CAPABILITY,options);

WebDriver driver = new ChromeDriver(caps);

对于火狐:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.cookie.cookieBehavior",2);
caps.setCapability(FirefoxDriver.PROFILE,profile);
于 2018-01-12T11:49:15.560 回答
1

对于 IE 以下作品-

禁用 cookie:

String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v       1A10 /t REG_DWORD /d 0X3 /f";  
Runtime.getRuntime().exec(command);  

启用 cookie:

String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v       1A10 /t REG_DWORD /d 0X1 /f";
Runtime.getRuntime().exec(command); 
于 2013-12-23T03:47:55.740 回答
1

对于 Chrome,请尝试以下操作:

DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);
于 2013-08-14T15:53:05.120 回答
1

当前 chrome 版本的首选项略有变化。

我目前使用的是 Chrome 版本 70。

在 Java 11 中。

var chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--incognito", "start-maximized"); // incognito and maximized.
var prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.cookies", 2);
chromeOptions.setExperimentalOption("prefs", prefs);

var webDriver = new ChromeDriver(chromeOptions);

根据官方文档

DesiredCapabilities不推荐用于 java,所以只需使用ChromeOptions.

引用自文档。

首选项:字典

包含首选项名称及其值的每个条目的字典。这些首选项仅适用于正在使用的用户配置文件。有关示例,请参阅 Chrome 用户数据目录中的“首选项”文件。

首选项文件是 C:\Users\用户名\AppData\Local\Google\Chrome\User Data\Default\Preferences

我刚刚在我的 chrome 中使用了设置,并看到了文件中的变化。

于 2018-11-20T19:18:40.277 回答