Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
David.M Avatar asked Aug 23 '17 14:08

David.M


People also ask

Can selenium handle cookies?

Selenium WebDriver provides multiple commands for handling the cookies in a website. These Selenium cookies APIs provide you the required mechanism for interacting and querying the cookies. In Selenium Java, these methods are a part of the org. openqa.

How do I enable cookies in selenium Python?

This lets your web browser store information like login information, username, shopping cart and more. The website needs to remember this between different pages, cookies are sometimes used for this. In selenium you can get and set cookies with the methods get_cookies() and add_cookie().

How do you check if a field is read only in selenium?

getAttribute("class"). equalsIgnoreCase("read-only")); Note that i'm accessing the element's class layer and verify that it is "read-only" using TestNG assertTrue after getting the element's attribute of "class". The asserTrue boolean will return false if the element is not read only.


2 Answers

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, e.g. 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.

like image 124
JeffC Avatar answered Oct 04 '22 19:10

JeffC


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.

like image 38
Nicholas DiPiazza Avatar answered Oct 04 '22 19:10

Nicholas DiPiazza