Home > Java > javaTutorial > body text

Clear browser cookies using Selenium WebDriver Java binding

WBOY
Release: 2023-08-25 21:53:01
forward
1596 people have browsed it

We can clear browser cookies in Selenium. Method deleteCookieNamed will delete cookies with a specific name. The name of the cookie to be deleted is passed as a parameter to this method. First we will add a cookie, then get it, and finally delete it.

Syntax

driver.manage().deleteCookieNamed("foo");
Copy after login

Another method called deleteAllCookies deletes all cookies in an existing domain. First we will add cookies, then get and delete them. The Chinese translation of

Syntax

driver.manage().deleteAllCookies();
Copy after login

Example

is:

Example

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class DeleteCookiesViaName{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // wait of 4 seconds
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // setting name and value for cookie
      Cookie c = new Cookie("test", "selenium");
      Cookie r = new Cookie("subject", "Java");
      // cookie addition
      driver.manage().addCookie(c);
      driver.manage().addCookie(r);
      // obtain the cookies
      Set<Cookie> ck = driver.manage().getCookies();
      //iterate through the cookies
      for (Cookie cookie : ck) {
         System.out.println("Cookie Name : "+cookie.getName());
         System.out.println("Cookie Value : "+cookie.getValue());
         // delete cookies by name
         driver.manage().deleteCookieNamed(cookie.getName());
      }
      // obtain the cookies after delete
      Set ch = driver.manage().getCookies();
      System.out.println("Cookie count after delete: "+ch.size());
   }
}
Copy after login

Example

The Chinese translation is:

Example

Code Implementation with deleteAllCookies.

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class DeleteCookiesAll{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // wait of 4 seconds
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // setting name and value for cookie
      Cookie c = new Cookie("test", "selenium");
      Cookie r = new Cookie("subject", "Java");
      // cookie addition
      driver.manage().addCookie(c);
      driver.manage().addCookie(r);
      // obtain the cookies
      Set<Cookie> ck = driver.manage().getCookies();
      //iterate through the cookies
      for (Cookie cookie : ck) {
         System.out.println("Cookie Name : "+cookie.getName());
         System.out.println("Cookie Value : "+cookie.getValue());
      }
      // delete cookies
      driver.manage().deleteAllCookies();
      // obtain the cookies after delete
      Set ch = driver.manage().getCookies();
      System.out.println("Cookie count after delete: "+ch.size());
   }
}
Copy after login

Output

使用Selenium WebDriver Java绑定清除浏览器Cookies

The above is the detailed content of Clear browser cookies using Selenium WebDriver Java binding. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template