问题 如何使用webdriver for Chrome和FireFox JAVA禁用cookie


我想要启动浏览器(FF,CHROME)进行禁用cookie测试,我试过这个:

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

但它不起作用......


7407
2017-08-07 14:48


起源



答案:


我刚刚获得Firefox的解决方案:

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

但我不知道如何使用Chrome管理它。


8
2017-08-07 15:14



cookiebehaviour,2是禁用?如何恢复它?是1还是0还是-1? - gumuruh
我猜,只是在没有这个选项的情况下启动它。 - jotrocken


答案:


我刚刚获得Firefox的解决方案:

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

但我不知道如何使用Chrome管理它。


8
2017-08-07 15:14



cookiebehaviour,2是禁用?如何恢复它?是1还是0还是-1? - gumuruh
我猜,只是在没有这个选项的情况下启动它。 - jotrocken


您可以停用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);

4
2017-12-23 03:43



2被禁用,如何启用? - gumuruh


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

DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);

1
2017-08-14 15:53





对于以下工作的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); 

1
2017-12-23 03:47





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

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

对于Chrome:

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

对于Firefox:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.cookie.cookieBehavior",2);
caps.setCapability(FirefoxDriver.PROFILE,profile);

1
2018-01-12 11:49