简体   繁体   中英

Unable to set cookies in Selenium Webdriver

I'm trying to add cookies to a link before I open it with webdriver but it keeps giving me this error:

org.openqa.selenium.UnableToSetCookieException: Unable to set cookie (WARNING: The server did not provide any stacktrace information)

Please find my code below:

System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example

Please help with a relevant solution.

You are creating the cookie before navigating to the site. If you are trying to create a cookie on the domain www.example.com, then you would want to navigate to some page on that domain, create the cookie, and then start your test.

From my reading a while back, the best way to do this is to navigate to some page you know will not exist on the domain, eg www.example.com/this404page, then create the cookie. It should load a lot faster since it's an error page and shouldn't contain much content. After creating the cookie on the 404 page, start your test.

You are unable to do this because the WebDriver spec requires that you have the browser landed at the domain you are trying to set cookies for.

The work-around as mentioned is to go to the page prior to setting the cookie. But that causes some issues:

This unfortunately prevents 2 key use cases:

  1. You want to re-use cookies from another webdriver session in a new session to avoid repeating the work it took to get the cookies. Such as having to repeat a login.
  2. Allow a webdriver pool to be shared amongst unrelated threads where each thread might have their own cookies.

The webdriver spec clearly needs to take this into account. I have opened an issue here:

https://github.com/w3c/webdriver/issues/1238

Give it some votes. All browsers should start carrying a way to handle this.

First navigate to URL and then try to add cookies, try below code:

System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example 

Replace your code with this :

System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example 
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);

Adding cookies before navigating to the domain are called domain-less cookies which i believe is not possible.

I have not found a way to drop a cookie before the url, but I think below scenario might help you out-

  1. Create a factory class to create webdriver instances
  2. Before returning the webdriver instance, navigating to any page on the domain under test, and drop the cookie, then navigate browser back.
  3. Your test can now begin, unaware that the navigation and cookie dropping has taken place

Just in case this is simpler, I did not need to create a factory class, merely load the root page from the site (which did not demand a cookie)

First collect the cookie as described above

pickle.dump(driver.get_cookies(), open("ChromeCookies.pkl", "wb"))

Then get the web site root which does not require a cookie (in my case)

driver.get(url_root)

Then execute the cookie load

for cookie in pickle.load(open("ChromeCookies.pkl", "rb")):
driver.add_cookie(cookie)

Then go to the page that I actually wanted to access

driver.get(url_not_root_demanding_cookie_which_is_now_there)

Please do post if there is an issue with this approach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM