如何使用 Chrome 和 FireFox JAVA 的 webdriver 禁用 cookie [英] how to disable cookies using webdriver for Chrome and FireFox JAVA

查看:14
本文介绍了如何使用 Chrome 和 FireFox JAVA 的 webdriver 禁用 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但它不起作用...

推荐答案

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

I've just get solution for Firefox:

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

对于 Chrome(阻止所有 cookie)

for Chrome (block all cookies)

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

适用于 Chrome(仅阻止第三方 cookie)

for Chrome (block only third party cookies)

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

这篇关于如何使用 Chrome 和 FireFox JAVA 的 webdriver 禁用 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆