1

I need to allow all cookies when running tests with selenium + chrome driver.

I am trying to add this as a profile preference using ChromeOptions.AddUserProfilePreference

I'm not 100% sure what the preference name should be to allow all cookies. I have referenced this doc https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup

and have tried the following in my setup but its not having the desired effect.

options.AddUserProfilePreference("profile.block_third_party_cookies", false);
options.AddUserProfilePreference("security.cookie_behavior", 0);```

Here is my setup code

                        new DriverManager().SetUpDriver(new ChromeConfig());
                        var options = new OpenQA.Selenium.Chrome.ChromeOptions { };
                        options.AddArgument("–no-sandbox");
                        options.AddArguments("-disable-gpu");
                        options.AddArguments("-disable-dev-shm-usage");
                        options.AddArgument("-incognito");
                        options.AddArgument("-start-maximized");
                        options.AddUserProfilePreference("security.cookie_behavior", 0);
                        CurrentWebDriver = new ChromeDriver(options);
RaSedg
  • 111
  • 1
  • 10

2 Answers2

2

I ran into the same issue. I found that using the following helped me:

options.AddUserProfilePreference("profile.cookie_controls_mode", 0);

The advice that helped me find this, was to check the Chrome preferences file (in my case C:\Users\<user>\AppData\Local\Google\Chrome\User Data\Default\Preferences). I saved a copy with cookies blocked, then changed the setting to allow all cookies and compared the two versions, and that highlighted the affected control for me.

Jason K
  • 71
  • 1
  • 8
0

Looks like in Chrome v86 third-party cookies disabled by default: Chromium SameSite Updates.

I found a workaround to enable third-party cookies on the browser new tab start page (I'm using inputsimulator library to open a new tab because other methods do not open the appropriate page).

Here is C# code for IWebDriver extension:

using WindowsInput;
using WindowsInput.Native;

public static void EnableThirdPartyCookies(this IWebDriver driver)
{
    var windowHandles = driver.WindowHandles;

    // Activate Browser window
    driver.SwitchTo().Window(driver.WindowHandles.Last());
    
    // Open New Tab Ctrl + T    
    new InputSimulator().Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_T);

    // Wait for open new tab
    const int retries = 100;
    for (var i = 0; i < retries; i++) 
    {
        Thread.Sleep(100);
        if (driver.WindowHandles.Count > windowHandles.Count)
            break;
    }

    // Enable Third Party Cookies
    if (driver.WindowHandles.Count > windowHandles.Count)
    {
        driver.Close();
        driver.SwitchTo().Window(driver.WindowHandles.Last());
        var selectedCookieControlsToggle = driver.FindElements(By.Id("cookie-controls-toggle"))
            .FirstOrDefault(x => x.GetAttribute("checked") != null);
        selectedCookieControlsToggle?.Click();
    }
}

Using:

var chromeService = ChromeDriverService.CreateDefaultService();
var options = new ChromeOptions { };
options.AddArgument("–no-sandbox");
options.AddArgument("-incognito");
options.AddArgument("-start-maximized");
var driver = new ChromeDriver(chromeService, options);
driver.EnableThirdPartyCookies();

Here is Java code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public void enableThirdPartyCookies(WebDriver driver) throws Exception {
    ArrayList<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());

    // Activate Browser window
    driver.switchTo().window(driver.getWindowHandle());

    // Open New Tab Ctrl + T
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_T);
    robot.keyRelease(KeyEvent.VK_T);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    // Wait for open new tab
    int retries = 100;
    for (int i = 0; i < retries; i++)
    {
        Thread.sleep(100);
        if (driver.getWindowHandles().size() > windowHandles.size())
            break;
    }

    // Enable Third Party Cookies
    if (driver.getWindowHandles().size() > windowHandles.size())
    {
        driver.close();
        windowHandles = new ArrayList<String> (driver.getWindowHandles());
        driver.switchTo().window(windowHandles.get(windowHandles.size() - 1));
        List list = driver.findElements(By.id("cookie-controls-toggle"));
        Optional<WebElement> selectedCookieControlsToggle = driver.findElements(By.id("cookie-controls-toggle")).stream()
                .filter(x -> x.getAttribute("checked") != null).findFirst();
        Optional.ofNullable(selectedCookieControlsToggle).get().get().click();
    }
}

Using:

ChromeOptions options = new ChromeOptions();
options.addArguments("–no-sandbox");
options.addArguments("incognito");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
enableThirdPartyCookies(driver);
G. Victor
  • 545
  • 1
  • 6
  • 17