0

I'm struggling with setting "document.cookie" value via Selenium. I want to achieve something like this. Generally, when I'm on a real browser like Chrome, I open the console and type

document.cookie='my_secret_cookie=1;path=/'

And everything works like a charm.

How can I achieve this via Selenium? Currently, I've done something like this, but it doesn't seem to work for me:

Date today    = new Date();
Date tomorrow = new Date(today.getTime() + (1000 * 60 * 60 * 24));
Cookie newCookie = new Cookie.Builder("myCookie","my_secret_cookie=1")
                .domain("my.site") // declared in my local.properties file
                .path("/")
                .expiresOn(tomorrow) 
                .isSecure(true) 
                .build(); 

driver.manage().addCookie(newCookie);

Any advice?

ajirebardyn
  • 43
  • 1
  • 6
  • Does this answer your question? [Unable to set cookies in Selenium Webdriver](https://stackoverflow.com/questions/45842709/unable-to-set-cookies-in-selenium-webdriver) – Ryan Wilson Apr 29 '22 at 15:23
  • @RyanWilson not really. When I'm trying to do it I'm getting the error: UnableToSetCookieException: unable to set cookie – ajirebardyn Apr 29 '22 at 16:44

0 Answers0