Selenium 简明教程

Selenium WebDriver - Handling Cookies

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

Selenium Webdriver can be used to handle cookies. We can add a cookie, obtain a cookie with a particular name, and delete a cookie with the help of various methods in Selenium Webdriver.

Basic Selenium Commands to Handle Cookies

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

Some of the methods on cookies are discussed below −

  1. addCookie() − This method is used to add a cookie to the current session.

  2. getCookieNamed() − This method is used to get a cookie with a particular name among all the created cookies. It yields none, if there is no cookie available with the name, passed as parameter.

  3. getCookies() − This method is used to get all the cookies for the current session. If the current browser session had terminated, this method would return an error.

  4. deleteCookieNamed() − This method is used to delete a cookie with a particular name, passed as a parameter to the method.

  5. deleteCookie() − This method is used to delete a cookie with the cookie object, passed as a parameter to the method.

  6. deleteAllCookies() − This method is used to delete all cookies from the current browser session.

Example 1 - Get & Add Cookies

让我们举一个示例,其中我们将添加 cookie 并获取为现有浏览上下文添加的 cookie 的详细信息。

Let us take an example where we would add and get the details of the cookie added for an existing browsing context.

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 ,表示代码成功执行。

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - Adding Multiple Cookies

让我们举一个例子,我们将添加多个 Cookie,然后获取为现有浏览上下文添加的所有 Cookie 详细信息。

Let us take an example where we would add multiple cookies and then get all the details of the cookie added for an existing browsing context.

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 的所有详细信息的示例。

Let us take an example where we would add multiple cookies and delete a cookie(with cookie name and object) and then get all the details of the cookie added for an existing browsing context.

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 详细信息。

In the above example, we had first added three cookies with names C1, C2, and C3. Then, deleted the cookies C3, and C2 with cookie name and object and retrieved the cookie details after deletion.

Example 4 - Adding Multiple Cookies Then Delete

我们举个添加多个 Cookie 然后删除为现有浏览上下文添加的所有 Cookie 的示例。

Let us take an example where we would add multiple cookies and then delete all the cookies added for an existing browsing context.

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。

In the above example, we had first added two cookies with names C1, and C2. Then, we had deleted all the cookies.