We can send cookies with Selenium webdriver. A cookie is stored in a key value pair. First, we have to add cookies, then can delete them. We can also get the cookies.
Also, we have to add import org.openqa.selenium.Cookie statement for cookie implementations.
Cookie ck = new Cookie("Automation", "QA"); driver.manage().addCookie(ck); driver.manage().getCookies(); driver.manage().deleteAllCookies();
Code Implementation
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.Cookie; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class CookiesSend{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // cookie set with name and value Cookie ck = new Cookie("Automation", "QA"); // cookie newly added driver.manage().addCookie(ck); // get all cookies Set<Cookie> cks = driver.manage().getCookies(); //iterate the cookies for (Cookie cook : ck) { System.out.println("Cookie name is: "+ cook.getName()); System.out.println("Cookie Value is : "+ cook.getValue()); } // delete all driver.manage().deleteAllCookies(); // get cookies Set chs = driver.manage().getCookies(); System.out.println("Cookie count after delete: "+chs.size()); driver.quit(); } }