首頁  >  文章  >  Java  >  使用Selenium WebDriver Java綁定清除瀏覽器Cookies

使用Selenium WebDriver Java綁定清除瀏覽器Cookies

WBOY
WBOY轉載
2023-08-25 21:53:011607瀏覽

我們可以在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刪除