What are the different cookies methods in Selenium?

Software TestingAutomation TestingSelenium Web Driver

The different cookies method in Selenium are listed below −

  • driver.manage().deleteAllCookies() − This removes all cookies.

  • driver.manage().deleteCookie(Id) − This removes a particular cookie.

  • driver.manage().deleteCookieNamed(CookieName) − This removes a particular cookie based on Name.

  • driver.manage().getCookies() − This returns all the cookies.

  • driver.manage().getCookieNamed(CookieName) − This returns a particular cookie based on Name.

  • driver.manage().addCookie(Id) − This adds a particular cookie.

Code Implementation with some cookies method.

Example

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CookiesScripting {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      System.setProperty("webdriver.chrome.driver",    "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().window().maximize();
      // deleting all the cookies
      driver.manage().deleteAllCookies();
      // delete a cookie of name sessionId
      driver.manage().deleteCookieNamed("sessionid");
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().window().maximize();
      driver.close();
   }
}
raja
Published on 10-Jun-2020 12:24:12
Advertisements