首页 > Java > java教程 > 使用Selenium WebDriver Java绑定清除浏览器Cookies

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

WBOY
发布: 2023-08-25 21:53:01
转载
1619 人浏览过

我们可以在Selenium中清除浏览器的cookie。方法deleteCookieNamed将删除具有特定名称的cookie。所需删除的cookie名称作为参数传递给该方法。首先,我们将添加一个cookie,然后获取它,最后删除它。

语法

driver.manage().deleteCookieNamed("foo");
登录后复制

另一种方法称为deleteAllCookies删除现有域中的所有cookie。首先,我们将添加cookie,然后获取并删除它们。

Syntax

driver.manage().deleteAllCookies();
登录后复制

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());
   }
}
登录后复制

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());
   }
}
登录后复制

输出

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

以上是使用Selenium WebDriver Java绑定清除浏览器Cookies的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板