0

I'm trying to set some cookies that I already have, to handle a request to a site using ChromeWebDriver and Selenium in Java:

System.setProperty("webdriver.chrome.driver", "/Users/test/Desktop/Configs/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().addCookie(new Cookie("mycookie","test"));
driver.get("https://www.google.com/");  

But I got the following exception: Exception in thread "main" org.openqa.selenium.UnableToSetCookieException: unable to set cookie. Of course, I'm trying with real cookies and they work as well in the browser...

Bondavalli69
  • 63
  • 1
  • 7

4 Answers4

1

Selenium is restricted from adding cookies to arbitrary domains. The domain of the cookie must equal the current browsing context's active document's domain. This behavior is described in the W3C WebDriver Specification and implemented by all major browser vendors. See: https://w3c.github.io/webdriver/#add-cookie

As a workaround, you can first navigate to any page on the domain and then add the cookies for that domain.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • Definitely did not know this. Once I navigated to a page, only then was I able to load my cookies with no errors! – kyrlon Jan 12 '23 at 03:26
0

Firstly navigate to URL and then try to add cookies, try below sequence:

System.setProperty("webdriver.chrome.driver", "/Users/test/Desktop/Configs/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().addCookie(new Cookie("mycookie","test"));

Update: After adding the cookie we can check that by displaying all the cookies.

Set<Cookie> cookiesList =  driver.manage().getCookies();
for(Cookie getcookies :cookiesList) {
   System.out.println(getcookies );
}
Rmahajan
  • 1,311
  • 1
  • 14
  • 23
  • In this way, when you invoke addCookies, you add cookies of the first request + my own cookies so this won't work... I'm saying this because I tried to get them: Set coo = driver.manage().getCookies(); coo.forEach(System.out::println); – Bondavalli69 Mar 30 '19 at 15:17
0

I agree with Ros5292 answer.

Okay, Now coming to your requirement, you need to delete cookie after opening the url as mentioned below:

driver.get("https://www.google.com/");
driver.manage().deleteAllCookies();
driver.manage().addCookie(new Cookie("mycookie","test"));
Set<Cookie> coo = driver.manage().getCookies(); 
coo.forEach(System.out::println); 

Optional:

One more thing, if you want to see the changes done by adding cookie explicity, you need to refresh your page(Right after adding it):

driver.get("https://www.google.com/");
driver.manage().deleteAllCookies();
driver.manage().addCookie(new Cookie("mycookie","test"));
driver.navigate().refresh();
Abhishek Dhoundiyal
  • 1,359
  • 1
  • 12
  • 19
0

As mentioned above, driver current URL must be in the same domain as the cookie. In addition to that, you can use javascript to set a cookie with Selenium just like in a normal browser:

driver.executeScript("document.cookie ="'cookie1=test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'");

If you want to add cookie to a list of cookies, you can do the following:

driver.executeScript("document.cookie ="document.cookie + 'cookie1=test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'");
Adi Ohana
  • 927
  • 2
  • 13
  • 18