How To Delete cookies in Selenium Webdriver

.
Question : How To Delete cookies in Selenium Webdriver  using Java ?

Answer :
To Delete cookies there are some predefined method available by using this we can clear the cookies .
  • deleteAllCookies();
  • deleteCookiesNamed();
  • deletecookies();

1) deleteAllCookies();
To Delete the all cookies for the current domain use this method .

Example
Example of deleteAllCookies method is given below
driver.manage().deleteAllCookies();

Selenium Code To delete all Cookies using java 

package automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClearCookies
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
driver.manage().deleteAllCookies();
}
}

2) deleteCookiesNamed();
This Method is used to delete the Cookies according to its name for the current domain.

Example
Example of deleteCookiesNamed () method is given below
driver.manage().deleteCookiesNamed("abc");

Selenium Code to Delete Cookies by its name using java

package automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClearCookies
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
driver.manage().deleteCookiesNamed("abc");
}
}

3) deleteCookies();
Delete Cookies  method  is used to delete a specific cookies.

Example
Example of deleteCookies () method given below
driver.manage().deleteCookies(arg0);

Selenium Code to Delete Specific Cookies using java


package automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClearCookies
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
driver.manage().deleteCookies(arg0);
}
}


In this tutorial we learn how to clear the cookies in selenium webdriver with various types .Hope this tutorial helpful for you ðŸ˜Š

Thanks & Regards 
Sandeep Yadav

Related Post :  

  • How to Launch Chrome browser In Selenium 
  • How to launch firefox browser in selenium 
  • How to launch Internet Explorer browser in selenium 
  • How to open the url in selenium


Importatnt Tags : 
How to delete all the cookies in selenium webdriver using java , How to clear the cookies in selenium webdriver using java , how to clear the Cookies in selenium webdriver , delete cookies in selenium
How To Delete cookies in Selenium Webdriver 




Post a Comment

0 Comments