Enabling Cookies in Chrome version 86 using Selenium

6.2k views Asked by At

I am trying to use Selenium with Java with update chrome version 86 for automation, but it shows third-party cookies disable. The site I am working on needs 3rd party cookies.

I tries to enable cookies using following code:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    options.addArguments("start-maximized");
    // options.addArguments("disable-infobars");
    options.addArguments("ignore-certificate-errors");
    options.addArguments("disable-popup-blocking");
    options.addArguments("disable-extensions");
    options.addArguments("disable-notifications");
    options.addArguments("no-sandbox");
    options.addArguments("allow-running-insecure-content");
    options.setAcceptInsecureCerts(true);

    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

    HashMap<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_settings.cookies", 0);
    options.setExperimentalOption("prefs", prefs);

    Boolean headless = Boolean.valueOf(ApplicationProperties.getInstance().getProperty("headless"));
    if (Boolean.TRUE.equals(headless)) {
        options.addArguments("headless");
        options.addArguments("window-size=1600x1200");
    }

    options.setCapability(ChromeOptions.CAPABILITY, options);
    options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
    UnexpectedAlertBehaviour.IGNORE);````

Not enabling cookies. Guide me what to do?

2

There are 2 answers

0
Sab On

Code in Kotlin the looks like

val prefs: MutableMap<String, Any> = HashMap()
prefs["profile.cookie_controls_mode"] = 0
chromeOptions.setExperimentalOption("prefs", prefs)
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions)
0
G. Victor On

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

Full answer here.

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