{"id":26814,"date":"2019-02-22T05:23:37","date_gmt":"2019-02-22T05:23:37","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=26814"},"modified":"2025-05-17T07:18:50","modified_gmt":"2025-05-17T07:18:50","slug":"how-to-add-cookie-with-selenium","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-add-cookie-with-selenium\/","title":{"rendered":"How to Add Cookie with Selenium"},"content":{"rendered":"<h1>How to Add Cookie with Selenium<\/h1>\n<p>Using Selenium we can add the cookie to the domain. To add a cookie, we should use a method named &#8220;addCookie(cookie)&#8221;<\/p>\n<h2>Cookie Parameters<\/h2>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>Key<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">name<\/code><\/td>\n<td>Cookie identifier (required)<\/td>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">value<\/code><\/td>\n<td>Cookie data (required)<\/td>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">domain<\/code><\/td>\n<td>Associated domain<\/td>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">path<\/code><\/td>\n<td>URL path (default: &#8216;\/&#8217;)<\/td>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">secure<\/code><\/td>\n<td>HTTPS-only flag<\/td>\n<\/tr>\n<tr>\n<td><code class=\"\" data-line=\"\">expiry<\/code><\/td>\n<td>Expiration timestamp<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>addCookie() Method<\/h2>\n<p><b>Method Name: addCookie(Cookie cookie)<\/b><br \/>\nSyntax:driver.manage().addCookie(arg0);<\/p>\n<p>Purpose: To add a specific cookie into cookies. If the cookie&#8217;s domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.<\/p>\n<p>Parameters: cookie &#8211; The name and value of the cookie to be added.<\/p>\n<h2>Example<\/h2>\n<pre><code class=\"\" data-line=\"\">@Test\n\tpublic void addCookie()\n\t{\n\t\tdriver= new FirefoxDriver();\n\t\tString URL=&lt;span class=&quot;hljs-string&quot;&gt;&quot;&lt;\/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;https:\/\/www.example.com\/&quot;&lt;\/span&gt;;\n\t\tdriver.navigate().to(URL);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/we should pass name and value for cookie as parameters\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ In this example we are passing, name=mycookie and value=123456789123\n\t\tCookie name = new Cookie(&lt;span class=&quot;hljs-string&quot;&gt;&quot;mycookie&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;cookiedata&quot;&lt;\/span&gt;);\n\t\tdriver.manage().addCookie(name);\n\t\t\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ After adding the cookie we will check that by displaying all the cookies.\n\t\tSet&lt;Cookie&gt; cookiesList =\u00a0 driver.manage().getCookies();\n\t\tfor(Cookie cookie :cookiesList) {\n\t\t\u00a0\u00a0\u00a0 System.out.println(cookie);\n\t\t}\n\t}<\/code><\/pre>\n<p>The output looks like below. We can see the added cookie along with the other cookies of the domain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Add Cookie with Selenium Using Selenium we can add the cookie to the domain. To add a cookie, we should use a method named &#8220;addCookie(cookie)&#8221; Cookie Parameters Key Description name Cookie identifier (required) value Cookie data (required) domain Associated domain path URL path (default: &#8216;\/&#8217;) secure HTTPS-only flag expiry Expiration timestamp &nbsp; addCookie() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[],"class_list":["post-26814","post","type-post","status-publish","format-standard","hentry","category-selenium","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=26814"}],"version-history":[{"count":7,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26814\/revisions"}],"predecessor-version":[{"id":27419,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/26814\/revisions\/27419"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=26814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=26814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=26814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}