how to disable cookies using webdriver for Chrome and FireFox JAVA

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I want launch browsers(FF, CHROME) for test with disabled cookies, I tried this:

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

but it's not work...

回答1:

I've just get solution for Firefox:

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

but I don't know how to manage it with Chrome.



回答2:

U can disable chrome cookies as below:

ChromeOptions options = new ChromeOptions();   Map prefs = new HashMap();   prefs.put("profile.default_content_settings.cookies", 2);   options.setExperimentalOptions("prefs", prefs); driver = new ChromeDriver(options);  


回答3:

For Chrome try the following:

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


回答4:

For IE following works-

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

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


回答5:

You can use the below code snippets to disable the cookies in the Chrome and Firefox browsers. If you wish to enable cookies, just remove the capability.

Safari doesn't support any capability to achieve this.

For Chrome:

DesiredCapabilities caps = new DesiredCapabilities();  ChromeOptions options = new ChromeOptions(); Map prefs = new HashMap(); Map profile = new HashMap(); Map contentSettings = new HashMap();  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);

For Firefox:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!