Cookie and selenium webdriver

To check Cookie value with selenium, we can use the following code –

package com.tanmaysarkar;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyCookie_01 {

	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args) {
		MyCookie_01 mc = new MyCookie_01();
		mc.launch_browser();
	}
	public void launch_browser(){
		try{
		 driver = new FirefoxDriver();
		 baseUrl = "http://www.tanmaysarkar.com";
		 driver.get(baseUrl);
		 driver.manage().window().maximize();
		 System.out.println("Open " + baseUrl);
		 Set<Cookie> cookies = driver.manage().getCookies();
		 Iterator<Cookie> itr = cookies.iterator();

		 while (itr.hasNext())
		 {
		     Cookie c = itr.next();
		     System.out.println("Cookie Name: " + c.getName()  + "\n\tCookie Domain: " + c.getDomain() +  "\n\tCookie Value: " + c.getValue() +  "\n\tPath: " + c.getPath()+  "\n\tExpiry Date: " + c.getExpiry()+  "\n\tSecure: " + c.isSecure());
		 }
		 Thread.sleep(3000);    
		 driver.quit();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *