Selenium 简明教程

Selenium WebDriver - Handling Cookies

Selenium Webdriver 可用于处理 cookie。我们可以借助 Selenium Webdriver 中的各种方法添加 cookie、获取特定名称的 cookie,以及删除 cookie。

Basic Selenium Commands to Handle Cookies

以下讨论了有关 cookie 的部分方法−

  1. addCookie() − 此方法用于向当前会话添加 cookie。

  2. getCookieNamed() − 此方法用于在所有创建的 cookie 中获取特定名称的 cookie。如果没有名称为参数的 cookie,它将生成无。

  3. getCookies() − 此方法用于获取当前会话的所有 cookie。如果当前浏览器会话已终止,此方法将返回错误。

  4. deleteCookieNamed() − 此方法用于删除特定名称的 cookie,该名称作为方法的参数传递。

  5. deleteCookie() − 此方法用于删除具有 cookie 对象的 cookie,该 cookie 对象作为方法的参数传递。

  6. deleteAllCookies() − 此方法用于从当前浏览器会话中删除所有 cookie。

Example 1 - Get & Add Cookies

让我们举一个示例,其中我们将添加 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://www.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
In the above example, we had first added a cookie with name C1 and then obtained its details as a message in the console - *Cookie details: C1=VAL1; path=/; domain=www.tutorialspoint.com;secure

sameSite=Lax*.

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

Example 2 - Adding Multiple Cookies

让我们举一个例子,我们将添加多个 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://www.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]
In the above example, we had first added multiple cookies with names C1 and C2 and then obtained its details as a message in the console - *Cookie details are: [C1=VAL1; path=/; domain=www.tutorialspoint.com;secure

sameSite=Lax, C2=VAL2; path=/; domain=www.tutorialspoint.com;secure;; sameSite=Lax]*.

Example 3 - Delete Cookies

我们举个添加多个 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://www.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, and C3 添加了三个 Cookie。然后,使用 Cookie 名称和对象删除 Cookie C3C2 并在删除后检索 Cookie 详细信息。

Example 4 - Adding Multiple Cookies Then Delete

我们举个添加多个 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://www.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。