我想要启动浏览器(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);
但它不起作用......
我刚刚获得Firefox的解决方案:
FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);
但我不知道如何使用Chrome管理它。
我刚刚获得Firefox的解决方案:
FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);
但我不知道如何使用Chrome管理它。
您可以停用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);
对于Chrome,请尝试以下操作:
DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);
对于以下工作的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);
您可以使用以下代码段来禁用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);