4

Anyone able to explain to me how I would be able to set cookies for a domain not visited with the use of a plugin with selenium for gecko driver? I have been trying to set a cookie to prevent seeing a login page, but the domain for the cookie is redirecting so I cannot set it by visiting it and cannot figure out how to do it.

I have tried this but looks as though I cannot specify this in selenium as I cannot visit this page.

Cookie cookie11 = new Cookie("SID",
                  "cookievalue",
                  ".google.com",
                  "/",
                  expiry1,
                  false,
                  false); 

Found a plugin called Cookies Export/import that I am trying to figure out if its possible to use to import the cookies from..

Any help would be appreciated!

Kabone
  • 85
  • 5
  • 13
  • Same question for Python: [How to save and load cookies using Python + Selenium WebDriver - Stack Overflow](https://stackoverflow.com/questions/15058462/how-to-save-and-load-cookies-using-python-selenium-webdriver) -- most methods there should work as well. – user202729 Dec 31 '20 at 00:24

2 Answers2

2

If you wish to use the specified extension in order to do this, I recommend looking at the SO Answer on How do you use a firefox plugin within a selenium webdriver program written in java? and you should be good from there.

However, I believe you can achieve this without using an extension, using addCookie() method.

WebDriver driver = new FirefoxDriver();

Cookie cookie = new Cookie("SID",
              "cookievalue",
              ".example.com",
              "/",
              expiry1,
              false,
              false); 

driver.manage().addCookie(cookie);
driver.get("http://www.example.com/login");

Assuming your cookie details are correct, you should be able to get past the login redirect.

See also: WebDriver – How to Restore Cookies in New Browser Window

dey.shin
  • 990
  • 10
  • 21
  • I've been across that link before, and it only shows how to load and specify the version, can't seem to find anything on how I might trigger an action within a plugin. The cookies I need to set are on .google and mail.google.com so by the time i visit them either has timed out or have forwarded. And from what I have been able to find all cookies must be set from the actual page so not sure if this will work.. I have tried with no avail – Kabone Oct 13 '17 at 15:33
  • 2
    Unfortunately, setting cookies like this causes exception `org.openqa.selenium.InvalidCookieDomainException: Document is cookie-averse` – Greg Witczak Nov 24 '20 at 22:40
2

You cannot do that. See https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie

I opened this issue with the spec https://github.com/w3c/webdriver/issues/1238

You need to rebuild the browser without those validations if you want to get passed this issue:

Here is the changes to make to FireFox (marionette) to get passed this: https://gist.github.com/nddipiazza/1c8cc5ec8dd804f735f772c038483401

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152