Wednesday, November 12, 2014


In this tutorial, I will tell you how to add cookie -

1. We need to create an object of cookie and pass the cookie name and value to the cookie's constructor as an argument.

2. Now add cookie using selenium webdriver.





Let's See Example

import java.util.Set;

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

public class Training {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("http:// www.flipkart. com/");

Cookie name = new Cookie("mycookie", "12345");
driver.manage().addCookie(name);

// Let's get the cookie's value
Set<cookie> cookies = driver.manage().getCookies();
for (Cookie getcookies : cookies) {
System.out.println(getcookies);
}

}

}







Output -