← back to articles

how to block third party cookies in chrome driver using selenium Archives - Ibrahim Shawahni

Save article ToRead Archive Delete · Log out

1 min read · View original · shawahni.co.nf

In this article I’m going to guide you through the easiest way to disable third party cookies while using Selenium web driver with Chrome driver.

disable third party cookies
Web browser cookies

How to disable third party cookies:

First we need to create a new project:

Create

Then we need to visit Selenium and download Selenium library for Java. Then we need to install chrome driver version 2.22. The latest until the date of this article.

after that, we need to create a new folder into the project. call it “lib”.

Drag and drop all the .jar files you have downloaded from the web.

Then drag and drop the chromerdriver.exe file into the “src” package.

After all these the above is done, we need to create a new JUnit test case.

it must contain at lease the following methods :
@before, @after, @test.

once every thing done.

project explorer image
the final content of project.

add the following code to your Project :

The following code will disable third party cookies

import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class RunChrome {

WebDriver driver;
final static int TIMEOUT = 30;
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ibra\\workspace\\DisableThirdparty\\src\\chromedriver.exe");

ChromeOptions options = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String,Object>();

prefs.put("profile.block_third_party_cookies", true);

options.setExperimentalOption("prefs",prefs);

DesiredCapabilities cap = DesiredCapabilities.chrome();

cap.setCapability(ChromeOptions.CAPABILITY,options);

driver = new ChromeDriver(cap);

driver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS);
}

@After
public void tearDown() throws Exception {
driver.quit();
}

@Test
public void test() throws InterruptedException {
driver.get("https://www.google.com");

Thread.sleep(10000);
}

}

After all above work is done, try to run the project as a JUnit.

When chrome driver is shown up it must contain an icon after the URL shows that the third party cookies are disabled.

disabled third party cookeis
disabled third party cookeis

IF you want a shorter way, please refer to this video.

it contains all above description, step by step.

Please feel free to contact me if you have any concerns or further questions.

you can communicate me any time I’m available 24/7.

thanks for reading, watching and posting a comment.