How to addcookie with Selenium Webdriver

Using webdriver we can easily pass the cookie to the domain. In order to pass cookie, we should use a method named "addCookie(cookie)"

Method Name: addCookie(Cookie cookie)
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name and value of the cookie to be add.


	public void addCookie()
		driver= new FirefoxDriver();
		String URL="";
                //we should pass name and value for cookie as parameters
                // In this example we are passing, name=mycookie and value=123456789123
		Cookie name = new Cookie("mycookie", "123456789123");
                // After adding the cookie we will check that by displaying all the cookies.
		Set<Cookie> cookiesList =  driver.manage().getCookies();
		for(Cookie getcookies :cookiesList) {
		    System.out.println(getcookies );

The output looks like below. We can see the added cookie along with the other cookies of the domain.


Selenium Tutorials: 


Its really a nice article....and hope few more complementary articles you may read on

Thank you !!

Awesome !!

Is there a way to add cookie before visiting target page?

Plz help me i am using it with dot net mvc and it is showing error : Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs.

Thank you for sharing code. It's really very userful and whenever i am not getting any issues i am opening your portal and checking awesome solutions.

Will you provide full code for cookies. I am getting error in adding cookies.

I need to disable a popup by providing cookie value.I fetched all the cookie values from the current domain.Which cookie name and value should I provide in emailSignup1 = new Cookie("name", "value") to disable the popup coming ?

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.