. Code My Day

Thursday, August 29, 2013

Getting all the cookie on a web page.

Getting all the cookie on a web page.

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://linkedin.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();
        }
    }
}

No comments:

Post a Comment