测试人社区

selenium-Working with cookies


title: “Working with cookies”
weight: 6

A cookie is a small piece of data that is sent from a website and stored in your computer.
Cookies are mostly used to recognise the user and load the stored information.

WebDriver API provides a way to interact with cookies with built-in methods:

Add Cookie

It is used to add a cookie to the current browsing context.
Add Cookie only accepts a set of defined serializable JSON object. Here
is the link to the list of accepted JSON key values

First of all, you need to be on the domain that the cookie will be
valid for. If you are trying to preset cookies before
you start interacting with a site and your homepage is large / takes a while to load
an alternative is to find a smaller page on the site (typically the 404 page is small,
e.g. http://example.com/some404page)

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class addCookie {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);

        // Adds the cookie into current browser context
        driver.manage().addCookie(new Cookie("key", "value"));
    } finally {
        driver.quit();
    }
}

}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“http://www.example.com”)

Adds the cookie into current browser context

driver.add_cookie({“name”: “key”, “value”: “value”})
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace AddCookie {
class AddCookie {
public static void Main(string args) {
IWebDriver driver = new ChromeDriver();
try {
// Navigate to Url
driver.Navigate().GoToUrl(“https://example.com”);

// Adds the cookie into current browser context
driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));

} finally {
driver.Quit();
}
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com

Adds the cookie into current browser context

driver.manage.add_cookie(name: “key”, value: “value”)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// set a cookie on the current domain
await driver.manage().addCookie({name:'key', value: 'value'});

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“https://example.com”)

    // Adds the cookie into current browser context
    driver.manage().addCookie(Cookie("key", "value"))
} finally {
    driver.quit()
}

}
{{< / code-panel >}}
{{< / code-tab >}}

Get Named Cookie

It returns the serialized cookie data matching with the cookie name among all associated cookies.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class getCookieNamed {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);
driver.manage().addCookie(new Cookie(“foo”, “bar”));

        // Get cookie details with named cookie 'foo'
        Cookie cookie1 = driver.manage().getCookieNamed("foo");
        System.out.println(cookie1);
    } finally {
        driver.quit();
    }
}

}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

driver = webdriver.Chrome()

Navigate to url

driver.get(“http://www.example.com”)

Adds the cookie into current browser context

driver.add_cookie({“name”: “foo”, “value”: “bar”})

Get cookie details with named cookie ‘foo’

print(driver.get_cookie(“foo”))
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace GetCookieNamed {
class GetCookieNamed {
public static void Main(string args) {
IWebDriver driver = new ChromeDriver();
try {
// Navigate to Url
driver.Navigate().GoToUrl(“https://example.com”);
driver.Manage().Cookies.AddCookie(new Cookie(“foo”, “bar”));

// Get cookie details with named cookie 'foo'
var cookie = driver.Manage().Cookies.GetCookieNamed("foo");
System.Console.WriteLine(cookie);

} finally {
driver.Quit();
}
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com
driver.manage.add_cookie(name: “foo”, value: “bar”)

Get cookie details with named cookie ‘foo’

puts driver.manage.cookie_named(‘foo’)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// set a cookie on the current domain
await driver.manage().addCookie({name:'foo', value: 'bar'});

// Get cookie details with named cookie 'foo' 
driver.manage().getCookie('foo').then(function (cookie) {
    console.log('cookie details => ', cookie);
});

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“https://example.com”)
driver.manage().addCookie(Cookie(“foo”, “bar”))

    // Get cookie details with named cookie 'foo'
    val cookie = driver.manage().getCookieNamed("foo")
    println(cookie)
} finally {
    driver.quit()
}

}
{{< / code-panel >}}
{{< / code-tab >}}

Get All Cookies

It returns a ‘successful serialized cookie data’ for current browsing context.
If browser is no longer available it returns error.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;

public class getAllCookies {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);
// Add few cookies
driver.manage().addCookie(new Cookie(“test1”, “cookie1”));
driver.manage().addCookie(new Cookie(“test2”, “cookie2”));

        // Get All available cookies
        Set<Cookie> cookies = driver.manage().getCookies();
        System.out.println(cookies);
    } finally {
        driver.quit();
    }
}

}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

driver = webdriver.Chrome()

Navigate to url

driver.get(“http://www.example.com”)

driver.add_cookie({“name”: “test1”, “value”: “cookie1”})
driver.add_cookie({“name”: “test2”, “value”: “cookie2”})

Get all available cookies

print(driver.get_cookies())
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace GetAllCookies {
class GetAllCookies {
public static void Main(string args) {
IWebDriver driver = new ChromeDriver();
try {
// Navigate to Url
driver.Navigate().GoToUrl(“https://example.com”);
driver.Manage().Cookies.AddCookie(new Cookie(“test1”, “cookie1”));
driver.Manage().Cookies.AddCookie(new Cookie(“test2”, “cookie2”));

// Get All available cookies
var cookies = driver.Manage().Cookies.AllCookies;

} finally {
driver.Quit();
}
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com
driver.manage.add_cookie(name: “test1”, value: “cookie1”)
driver.manage.add_cookie(name: “test2”, value: “cookie2”)

Get all available cookies

puts driver.manage.all_cookies
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// Add few cookies
await driver.manage().addCookie({name:'test1', value:'cookie1'});
await driver.manage().addCookie({name:'test2', value:'cookie2'});

// Get all Available cookies
driver.manage().getCookies().then(function (cookies) {
    console.log('cookie details => ', cookies);
});

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“https://example.com”)
driver.manage().addCookie(Cookie(“test1”, “cookie1”))
driver.manage().addCookie(Cookie(“test2”, “cookie2”))

    // Get All available cookies
    val cookies = driver.manage().cookies
    println(cookies)
} finally {
    driver.quit()
}

}
{{< / code-panel >}}
{{< / code-tab >}}

Delete Cookie

It deletes the cookie data matching with the provided cookie name.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class deleteCookie {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);
driver.manage().addCookie(new Cookie(“test1”, “cookie1”));
Cookie cookie1 = new Cookie(“test2”, “cookie2”);
driver.manage().addCookie(cookie1);

        // delete a cookie with name 'test1'
        driver.manage().deleteCookieNamed("test1");

        /*
         Selenium Java bindings also provides a way to delete
         cookie by passing cookie object of current browsing context
         */
        driver.manage().deleteCookie(cookie1);
    } finally {
        driver.quit();
    }
}

}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver
driver = webdriver.Chrome()

Navigate to url

driver.get(“http://www.example.com”)
driver.add_cookie({“name”: “test1”, “value”: “cookie1”})
driver.add_cookie({“name”: “test2”, “value”: “cookie2”})

Delete a cookie with name ‘test1’

driver.delete_cookie(“test1”)
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace DeleteCookie {
class DeleteCookie {
public static void Main(string args) {
IWebDriver driver = new ChromeDriver();
try {
// Navigate to Url
driver.Navigate().GoToUrl(“https://example.com”);
driver.Manage().Cookies.AddCookie(new Cookie(“test1”, “cookie1”));
var cookie = new Cookie(“test2”, “cookie2”);
driver.Manage().Cookies.AddCookie(cookie);

// delete a cookie with name 'test1'	
driver.Manage().Cookies.DeleteCookieNamed("test1");

// Selenium .net bindings also provides a way to delete
// cookie by passing cookie object of current browsing context
driver.Manage().Cookies.DeleteCookie(cookie);

} finally {
driver.Quit();
}
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com
driver.manage.add_cookie(name: “test1”, value: “cookie1”)
driver.manage.add_cookie(name: “test2”, value: “cookie2”)

delete a cookie with name ‘test1’

driver.manage.delete_cookie(‘test1’)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// Add few cookies
await driver.manage().addCookie({name:'test1', value:'cookie1'});
await driver.manage().addCookie({name:'test2', value:'cookie2'});

// Delete a cookie with name 'test1'
await driver.manage().deleteCookie('test1');

// Get all Available cookies
driver.manage().getCookies().then(function (cookies) {
    console.log('cookie details => ', cookies);
});

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“https://example.com”)
driver.manage().addCookie(Cookie(“test1”, “cookie1”))
val cookie1 = Cookie(“test2”, “cookie2”)
driver.manage().addCookie(cookie1)

    // delete a cookie with name 'test1'
    driver.manage().deleteCookieNamed("test1")
    
    // delete cookie by passing cookie object of current browsing context.
    driver.manage().deleteCookie(cookie1)
} finally {
    driver.quit()
}

}
{{< / code-panel >}}
{{< / code-tab >}}

Delete All Cookies

It deletes all the cookies of the current browsing context.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class deleteAllCookies {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);
driver.manage().addCookie(new Cookie(“test1”, “cookie1”));
driver.manage().addCookie(new Cookie(“test2”, “cookie2”));

        // deletes all cookies
        driver.manage().deleteAllCookies();
    } finally {
        driver.quit();
    }
}

}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver
driver = webdriver.Chrome()

Navigate to url

driver.get(“http://www.example.com”)
driver.add_cookie({“name”: “test1”, “value”: “cookie1”})
driver.add_cookie({“name”: “test2”, “value”: “cookie2”})

Deletes all cookies

driver.delete_all_cookies()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace DeleteAllCookies {
class DeleteAllCookies {
public static void Main(string args) {
IWebDriver driver = new ChromeDriver();
try {
// Navigate to Url
driver.Navigate().GoToUrl(“https://example.com”);
driver.Manage().Cookies.AddCookie(new Cookie(“test1”, “cookie1”));
driver.Manage().Cookies.AddCookie(new Cookie(“test2”, “cookie2”));

// deletes all cookies
driver.Manage().Cookies.DeleteAllCookies();

} finally {
driver.Quit();
}
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com
driver.manage.add_cookie(name: “test1”, value: “cookie1”)
driver.manage.add_cookie(name: “test2”, value: “cookie2”)

deletes all cookies

driver.manage.delete_all_cookies
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// Add few cookies
await driver.manage().addCookie({name:'test1', value:'cookie1'});
await driver.manage().addCookie({name:'test2', value:'cookie2'});

// Delete all cookies
await driver.manage().deleteAllCookies();

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“https://example.com”)
driver.manage().addCookie(Cookie(“test1”, “cookie1”))
driver.manage().addCookie(Cookie(“test2”, “cookie2”))

    // deletes all cookies
    driver.manage().deleteAllCookies()
} finally {
    driver.quit()
}

}
{{< / code-panel >}}
{{< / code-tab >}}

Same-Site Cookie Attribute

It allows a user to instruct browsers to control whether cookies
are sent along with the request initiated by third party sites.
It is introduced to prevent CSRF (Cross-Site Request Forgery) attacks.

Same-Site cookie attribute accepts two parameters as instructions

Strict:

When the sameSite attribute is set as Strict,
the cookie will not be sent along with
requests initiated by third party websites.

Lax:

When you set a cookie sameSite attribute to Lax,
the cookie will be sent along with the GET
request initiated by third party website.

Note: As of now this feature is landed in chrome(80+version),
Firefox(79+version) and works with Selenium 4 and later versions.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class cookieTest {
public static void main(String args) {
WebDriver driver = new ChromeDriver();
try {
driver.get(“http://www.example.com”);
Cookie cookie = new Cookie.Builder(“key”, “value”).sameSite(“Strict”).build();
Cookie cookie1 = new Cookie.Builder(“key”, “value”).sameSite(“Lax”).build();
driver.manage().addCookie(cookie);
driver.manage().addCookie(cookie1);
System.out.println(cookie.getSameSite());
System.out.println(cookie1.getSameSite());
} finally {
driver.quit();
}
}
}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“http://www.example.com”)

Adds the cookie into current browser context with sameSite ‘Strict’ (or) ‘Lax’

driver.add_cookie({“name”: “foo”, “value”: “value”, ‘sameSite’: ‘Strict’})
driver.add_cookie({“name”: “foo1”, “value”: “value”, ‘sameSite’: ‘Lax’})
cookie1 = driver.get_cookie(‘foo’)
cookie2 = driver.get_cookie(‘foo1’)
print(cookie1)
print(cookie2)
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
// Please raise a PR
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :chrome

begin
driver.get ‘https://www.example.com

Adds the cookie into current browser context with sameSite ‘Strict’ (or) ‘Lax’

driver.manage.add_cookie(name: “foo”, value: “bar”, same_site: “Strict”)
driver.manage.add_cookie(name: “foo1”, value: “bar”, same_site: “Lax”)
puts driver.manage.cookie_named(‘foo’)
puts driver.manage.cookie_named(‘foo1’)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder} = require(‘selenium-webdriver’);
(async function example() {
let driver = new Builder()
.forBrowser(‘chrome’)
.build();

await driver.get('https://www.example.com');

// set a cookie on the current domain with sameSite 'Strict' (or) 'Lax'
await driver.manage().addCookie({name:'key', value: 'value', sameSite:'Strict'});
await driver.manage().addCookie({name:'key', value: 'value', sameSite:'Lax'});
console.log(await driver.manage().getCookie('key'));

})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Cookie
import org.openqa.selenium.chrome.ChromeDriver

fun main() {
val driver = ChromeDriver()
try {
driver.get(“http://www.example.com”)
val cookie = Cookie.Builder(“key”, “value”).sameSite(“Strict”).build()
val cookie1 = Cookie.Builder(“key”, “value”).sameSite(“Lax”).build()
driver.manage().addCookie(cookie)
driver.manage().addCookie(cookie1)
println(cookie.getSameSite())
println(cookie1.getSameSite())
} finally {
driver.quit()
}
}
{{< / code-panel >}}
{{< / code-tab >}}

官方链接为:https://www.selenium.dev/documentation/en/support_packages/working_with_cookies