クッキーの使用

Cookieは、Webサイトから送信され、コンピューターに保存される小さなデータです。 Cookieは、主にユーザーを認識し、保存されている情報を読み込むために使用されます。

WebDriver APIは、組み込みメソッドでCookieと対話するメソッドを提供します。

クッキーの追加

現在のブラウジングコンテキストにCookieを追加するために使用されます。 Cookieの追加では、一連の定義済みのシリアル化可能なJSONオブジェクトのみを受け入れます。 受け入れられたJSONキー値のリストへのリンクはこちらにあります。

まず、Cookieが有効になるドメインにいる必要があります。 サイトとの対話を開始する前にCookieを事前設定しようとしていて、ホームページが大きい場合/代替の読み込みに時間がかかる場合は、サイトで小さいページを見つけることです。(通常、たとえば http://example.com/some404page のような、404ページは小さいです。)

	      driver.get("https://www.selenium.dev/selenium/web/blank.html");
	      // Add cookie into current browser context
	      driver.manage().addCookie(new Cookie("key", "value"));
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Adds the cookie into current browser context</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;key&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">})</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L5-L9" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
         driver.Url="https://www.selenium.dev/selenium/web/blank.html";
         // Add cookie into current browser context
         driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));
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
  
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()
    }
}  
  

命名されたクッキーの取得

関連付けられているすべてのCookieの中で、Cookie名と一致するシリアル化されたCookieデータを返します。

	        driver.get("https://www.selenium.dev/selenium/web/blank.html");
	        // Add cookie into current browser context
	        driver.manage().addCookie(new Cookie("foo", "bar"));
	        // Get cookie details with named cookie 'foo'
	        Cookie cookie = driver.manage().getCookieNamed("foo");
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Adds the cookie into current browser context</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;foo&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;bar&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Get cookie details with named cookie &#39;foo&#39;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get_cookie</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;foo&#34;</span><span style="color:#000;font-weight:bold">))</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L13-L20" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
         driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
         // Add cookie into current browser context
         driver.Manage().Cookies.AddCookie(new Cookie("foo", "bar"));
         // Get cookie details with named cookie 'foo'
         Cookie cookie = driver.Manage().Cookies.GetCookieNamed("foo");
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
  
    await driver.manage().addCookie({ name: 'foo', value: 'bar' });

    // Get cookie details with named cookie 'foo'
    await driver.manage().getCookie('foo').then(function(cookie) {
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()
    }
}  
  

全てのクッキーの取得

現在のブラウジングコンテキストの ‘成功したシリアル化されたCookieデータ’ を返します。 ブラウザが使用できなくなった場合、エラーが返されます。

	        driver.get("https://www.selenium.dev/selenium/web/blank.html");
	        // Add cookies into current browser context
	        driver.manage().addCookie(new Cookie("test1", "cookie1"));
	        driver.manage().addCookie(new Cookie("test2", "cookie2"));
	        // Get cookies
	        Set<Cookie> cookies = driver.manage().getCookies();
	         for (Cookie cookie : cookies) {
	            if (cookie.getName().equals("test1")) {
	                Assertions.assertEquals(cookie.getValue(), "cookie1");
	            }

	            if (cookie.getName().equals("test2")) {
	                Assertions.assertEquals(cookie.getValue(), "cookie2");
	            }
	         }
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test1&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie1&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test2&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie2&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Get all available cookies</span>
</span></span><span style="display:flex;"><span>    <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get_cookies</span><span style="color:#000;font-weight:bold">())</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L24-L32" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
         driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
         // Add cookies into current browser context
         driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
         driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
         // Get cookies
         var cookies = driver.Manage().Cookies.AllCookies;
         foreach (var cookie in cookies){
             if (cookie.Name.Equals("test1")){
                 Assert.AreEqual("cookie1", cookie.Value);
             }
             if (cookie.Name.Equals("test2")){
                 Assert.AreEqual("cookie2", cookie.Value);
             }
         }
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
  

    // Get all Available cookies
    await driver.manage().getCookies().then(function(cookies) {
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()
    }
}  
  

クッキーの削除

指定されたCookie名と一致するCookieデータを削除します。

	        driver.get("https://www.selenium.dev/selenium/web/blank.html");
	        driver.manage().addCookie(new Cookie("test1", "cookie1"));
	        // delete cookie named
	        driver.manage().deleteCookieNamed("test1");
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test1&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie1&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test2&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie2&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Delete cookie with name &#39;test1&#39;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">delete_cookie</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;test1&#34;</span><span style="color:#000;font-weight:bold">)</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L35-L43" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
         driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
         driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
         // delete cookie named
         driver.Manage().Cookies.DeleteCookieNamed("test1");
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
  
    await driver.manage().addCookie({ name: 'test2', value: 'cookie2' });
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()
    }
}  
  

全てのクッキーの削除

現在のブラウジングコンテキストの全てのCookieを削除します。

	        driver.get("https://www.selenium.dev/selenium/web/blank.html");
	        // Add cookies into current browser context
	        driver.manage().addCookie(new Cookie("test1", "cookie1"));
	        driver.manage().addCookie(new Cookie("test2", "cookie2"));
	        // Delete All cookies
	        driver.manage().deleteAllCookies();
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test1&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie1&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;test2&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;cookie2&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Delete all cookies</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">delete_all_cookies</span><span style="color:#000;font-weight:bold">()</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L47-L55" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
         driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
         // Add cookies into current browser context
         driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
         driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
         // Delete All cookies
         driver.Manage().Cookies.DeleteAllCookies();
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
  
    await driver.manage().addCookie({ name: 'test2', value: 'cookie2' });
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()
    }
}  
  

SameSite Cookie属性

これにより、ユーザーは、サードパーティのサイトによって開始されたリクエストとともに Cookieを送信するかどうかをブラウザに指示できます。 CSRF(クロスサイトリクエストフォージェリ)攻撃を防ぐために導入されました。

SameSite Cookie属性は、2つのパラメーターを命令として受け入れます。

Strict:

SameSite属性が Strict に設定されている場合、CookieはサードパーティのWebサイトによって 開始されたリクエストとともに送信されません。

Lax:

CookieのSameSite属性を Lax に設定すると、CookieはサードパーティのWebサイトによって 開始されたGETリクエストとともに送信されます。

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

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();
    }
  }
}
  
  




  




  













  
  
  

  
  
  

  
  
    
    
    
    
    
    
  

  <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span>    <span style="color:#000">driver</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">webdriver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">Chrome</span><span style="color:#000;font-weight:bold">()</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;http://www.example.com&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#8f5902;font-style:italic"># Adds the cookie into current browser context with sameSite &#39;Strict&#39; (or) &#39;Lax&#39;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;foo&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;sameSite&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Strict&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">add_cookie</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;foo1&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;value&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;sameSite&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Lax&#34;</span><span style="color:#000;font-weight:bold">})</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">cookie1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get_cookie</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;foo&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#000">cookie2</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">driver</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">get_cookie</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;foo1&#34;</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">cookie1</span><span style="color:#000;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">cookie2</span><span style="color:#000;font-weight:bold">)</span></span></span></code></pre></div>

  <div class="text-end pb-2">
    <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/release-4-27/examples/python/tests/interactions/test_cookies.py#L59-L71" target="_blank">
      <i class="fas fa-external-link-alt pl-2"></i>
        <strong>View full example on GitHub</strong>
    </a>
  </div>




  
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SameSiteCookie {
  class SameSiteCookie {
    static void Main(string[] args) {
      IWebDriver driver = new ChromeDriver();
      try {
        driver.Navigate().GoToUrl("http://www.example.com");

        var cookie1Dictionary = new System.Collections.Generic.Dictionary<string, object>() {
          { "name", "test1" }, { "value", "cookie1" }, { "sameSite", "Strict" } };
        var cookie1 = Cookie.FromDictionary(cookie1Dictionary);

        var cookie2Dictionary = new System.Collections.Generic.Dictionary<string, object>() {
          { "name", "test2" }, { "value", "cookie2" }, { "sameSite", "Lax" } };
        var cookie2 = Cookie.FromDictionary(cookie2Dictionary);

        driver.Manage().Cookies.AddCookie(cookie1);
        driver.Manage().Cookies.AddCookie(cookie2);

        System.Console.WriteLine(cookie1.SameSite);
        System.Console.WriteLine(cookie2.SameSite);
      } finally {
        driver.Quit();
      }
    }
  }
}
  
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
  
    await driver.get('https://www.selenium.dev/selenium/web/blank.html');

    // set a cookie on the current domain with sameSite 'Strict' (or) 'Lax'
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()
    }
}