• Selenium Video Tutorials

Selenium WebDriver - 处理Cookie



Selenium Webdriver 可用于处理 Cookie。我们可以使用 Selenium Webdriver 中的各种方法添加 Cookie,获取具有特定名称的 Cookie 以及删除 Cookie。

处理 Cookie 的基本 Selenium 命令

下面讨论了一些关于 Cookie 的方法:

  • addCookie() - 此方法用于向当前会话添加 Cookie。
  • getCookieNamed() - 此方法用于在所有创建的 Cookie 中获取具有特定名称的 Cookie。如果不存在具有作为参数传递的名称的 Cookie,则返回空值。
  • getCookies() - 此方法用于获取当前会话的所有 Cookie。如果当前浏览器会话已终止,此方法将返回错误。
  • deleteCookieNamed() - 此方法用于删除具有特定名称的 Cookie,该名称作为参数传递给该方法。
  • deleteCookie() - 此方法用于使用作为参数传递给该方法的 Cookie 对象删除 Cookie。
  • deleteAllCookies() - 此方法用于删除当前浏览器会话中的所有 Cookie。

示例 1 - 获取和添加 Cookie

让我们来看一个示例,在这个示例中,我们将为现有的浏览上下文添加 Cookie 并获取添加的 Cookie 的详细信息。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class AndGetCookie {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // open browser session
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Add cookie in key-value pairs
      driver.manage().addCookie(new Cookie("C1", "VAL1"));

      // Get added cookie with name
      Cookie c1 = driver.manage().getCookieNamed("C1");
      System.out.println("Cookie details: " + c1);

      // Closing browser
      driver.quit();
   }
}

输出

Cookie details: C1=VAL1; path=/; 
domain=www.tutorialspoint.com;secure;; sameSite=Lax

Process finished with exit code 0

在上面的示例中,我们首先添加了一个名为 C1 的 Cookie,然后在控制台中获取其详细信息作为消息 - Cookie details: C1=VAL1; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax

最后,收到消息 Process finished with exit code 0,表示代码成功执行。

示例 2 - 添加多个 Cookie

让我们来看一个示例,在这个示例中,我们将添加多个 Cookie,然后获取为现有的浏览上下文添加的 Cookie 的所有详细信息。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class AndGetCookies {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // open browser session
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Add two cookies in key-value pairs
      driver.manage().addCookie(new Cookie("C1", "VAL1"));
      driver.manage().addCookie(new Cookie("C2", "VAL2"));

      // get every cookie details
      Set<Cookie> c = driver.manage().getCookies();
      System.out.println("Cookie details are: " + c);

      // Closing browser
      driver.quit();
   }
}

输出

Cookie details are: [C1=VAL1; path=/;
domain=www.tutorialspoint.com;secure;; sameSite=Lax, C2=VAL2; path=/; 
domain=www.tutorialspoint.com;secure;; sameSite=Lax]

在上面的示例中,我们首先添加了名为 C1C2 的多个 Cookie,然后在控制台中获取其详细信息作为消息 - Cookie details are: [C1=VAL1; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax, C2=VAL2; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax]

示例 3 - 删除 Cookie

让我们来看一个示例,在这个示例中,我们将添加多个 Cookie,删除一个 Cookie(使用 Cookie 名称和对象),然后获取为现有的浏览上下文添加的 Cookie 的所有详细信息。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class AndDeleteCookies {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // open browser session
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Add three cookies in key-value pairs
      driver.manage().addCookie(new Cookie("C1", "VAL1"));
      Cookie m = new Cookie("C2", "VAL2");
      driver.manage().addCookie(m);
      driver.manage().addCookie(new Cookie("C3", "VAL3"));

      // get every cookie details
      Set<Cookie> c = driver.manage().getCookies();
      System.out.println("Cookie details are: " + c);

      // delete a cookie with its name
      driver.manage().deleteCookieNamed("C3");

      // get every cookie details after deleting cookie C3
      Set<Cookie> c1 = driver.manage().getCookies();
      System.out.println("Cookie details after deleting C3 cookie are: " + c1);

      // delete a cookie with cookie object
      driver.manage().deleteCookie(m);

      // get every cookie details after deleting cookie C2
      Set<Cookie> c2 = driver.manage().getCookies();
      System.out.println("Cookie details after deleting C2 cookie are: " + c2);

      // Closing browser
      driver.quit();
   }
}

输出

Cookie details are: [C3=VAL3; path=/; domain=www.tutorialspoint.com;secure;;
sameSite=Lax, C1=VAL1; path=/; domain=www.tutorialspoint.com;secure;; 
sameSite=Lax, C2=VAL2; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax]
Cookie details after deleting C3 cookie are: [C1=VAL1; path=/; 
domain=www.tutorialspoint.com;secure;; sameSite=Lax, C2=VAL2; 
path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax]
Cookie details after deleting C2 cookie are: 
[C1=VAL1; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax]

在上面的示例中,我们首先添加了名为 C1、C2 和 C3 的三个 Cookie。然后,使用 Cookie 名称和对象删除了 Cookie C3C2,并在删除后检索了 Cookie 详细信息。

示例 4 - 添加多个 Cookie 然后删除

让我们来看一个示例,在这个示例中,我们将添加多个 Cookie,然后删除为现有的浏览上下文添加的所有 Cookie。

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class AndDeleteAllCookies {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // open browser session
      driver.get("https://tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Add two cookies in key-value pairs
      driver.manage().addCookie(new Cookie("C1", "VAL1"));
      driver.manage().addCookie(new Cookie("C3", "VAL3"));

      // get every cookie details
      Set<Cookie> c = driver.manage().getCookies();
      System.out.println("Cookie details are: " + c);

      // delete a cookie with its name
      driver.manage().deleteAllCookies();

      // get every cookie details after deleting cookie C2
      Set<Cookie> c2 = driver.manage().getCookies();
      System.out.println("Cookie details after deleting all cookies: " + c2);

      // Closing browser
      driver.quit();
   }
}

输出

Cookie details are: [C3=VAL3; path=/; domain=www.tutorialspoint.com;secure;;
   sameSite=Lax, C1=VAL1; path=/; 
   domain=www.tutorialspoint.com;secure;; sameSite=Lax]
Cookie details after deleting all cookies: []

在上面的示例中,我们首先添加了名为 C1C2 的两个 Cookie。然后,我们删除了所有 Cookie。

广告