Handle cookies in Selenium WebDriver

In the previous post, we have seen how to work with multiple browser windows.In this post, we will learn how to Handle cookies in Selenium WebDriver.

What is a cookie?

Cookies are small text files which gets stored with browser’s directory. When we open any website in a browser, then cookie is used to keep track of the movements in the site, guide us to resume where we it was left off, other customization function, remember registered login, theme selection etc.

Deleting cookies may help in some following ways:

Deleting cookies is not very important, but it depends on the types of sites you deal with.

  • It may Speed up computer.
  • Cookies sometimes can redirect you to undesired sites
  • You maybe concerned about your privacy. If you visit sites you don’t want others to know, deleting cookies is safe and good.

 

Selenium provides methods to add or delete cookies. Let’s understand with examples.

1. getCookies(): This helps to fetch all the cookies available in the website. It will give a set (collection) of cookies, which you can iterate over to see its details.

Code to display all the cookies available in Facebook WebSite:

public class getCookiesExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("http://facebook.com");
		Set<Cookie> cookie = driver.manage().getCookies();
		
		for(Cookie c : cookie){
			System.out.println(c);
		}
 	}
}

Output:

_js_reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com _js_reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com wd=1280×693; path=/; domain=.facebook.com _js_datr=5jjSVYpvPWq_1ebdMFcfqmz6; expires=Mon, 02 Apr 2063 08:52:54 IST; path=/; domain=.facebook.com reg_ext_ref=; path=/; domain=.facebook.com

Don't Forget Note Means Important Remember Forgetting

Cookie usually stores the following information:
– The name of cookie.
– The value of the cookie.
– Domain the cookie is valid for.
– The expiration date of the cookie.
– The path the cookie is valid for.Web pages outside of that path cannot use the cookie.

 

2. addCookie(): This method helps you to add the cookie into the existing cookies. Cookie can be added in a pair of String Key and value.If the cookie’s domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.

Syntax: Syntax:driver.manage().addCookie(arg0);

In the below example, we are adding cookie with String “This is my cookie.” and value “12345“.

public class getCookiesExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("http://facebook.com");
		
		Cookie name = new Cookie("This is my cookie.", "123456");
		driver.manage().addCookie(name);
		
		Set<Cookie> cookie = driver.manage().getCookies();
		
		for(Cookie c : cookie){
			System.out.println(c);
		}
 	}
}

Output:

This is my cookie.=123456; path=/; domain=www.facebook.com _js_reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com _js_reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com wd=1280×693; path=/; domain=.facebook.com _js_datr=ZkDSVZ8Fn5wuHTrdRxjDFRaS; expires=Mon, 02 Apr 2063 09:56:54 IST; path=/; domain=.facebook.com reg_ext_ref=; path=/; domain=.facebook.com

 

3. deleteCookieNamed(): Need to provide the name of the cookie which needs to be deleted. In te above cookie output , we have one cookie “_js_reg_fb_gate”. Let’s observe the output.

public class getCookiesExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("http://facebook.com");
		
		Cookie name = new Cookie("This is my cookie.", "123456");
		driver.manage().addCookie(name);
		
		driver.manage().deleteCookieNamed("_js_reg_fb_gate");
		Set<Cookie> cookie = driver.manage().getCookies();
		
		for(Cookie c : cookie){
			System.out.println(c);
		}
 	}
}
This is my cookie.=123456; path=/; domain=www.facebook.com
_js_reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F; path=/; domain=.facebook.com
wd=1280×693; path=/; domain=.facebook.com
_js_datr=Y0PSVQtDszsr0zGAz9N-hQsB; expires=Mon, 02 Apr 2063 10:22:24 IST; path=/; domain=.facebook.com
reg_ext_ref=; path=/; domain=.facebook.com

Specified cookie has been deleted in the output.


4. deleteAllCookies(): It will delete all the cookies available in the Website. Let’s observe in the program.

	public class getCookiesExample {
		private static WebDriver driver;
		public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("http://facebook.com");
			
			Cookie name = new Cookie("This is my cookie.", "123456");
			driver.manage().addCookie(name);
			
			driver.manage().deleteAllCookies();
			Set<Cookie> cookie = driver.manage().getCookies();
			
			for(Cookie c : cookie){
				System.out.println(c);
			}
	 	}
	}

Output: All cookies will be deleted.


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...