Hi I want to login using cookies in selenium webdriver I first logged in to site using test script 1 and then trying to login using stored cookies using test script 2 It is not working Below is the code :

script 1)

     //save login credentials in cookies
    ChromeOptions options = new ChromeOptions();
    WebDriver driver = new ChromeDriver(options);   



    try {
    File f= new File ("D:\\browser.txt");
        FileWriter fs= new FileWriter(f);
        BufferedWriter  bw= new BufferedWriter(fs);

        Set<Cookie> myCookies =driver.manage().getCookies();
        for(Cookie ck:myCookies)

    } catch (IOException e) {


Script 2)

    try {

    File f= new File ("D:\\browser.txt");
    FileReader fr= new FileReader(f);

    BufferedReader  br= new BufferedReader(fr);

    String sCurrentLine;

    while (( sCurrentLine = br.readLine()) != null) 
        String[] strArr = sCurrentLine.split(";");

            for (int i=0;i< strArr.length;)


                    String name= strArr[i];
                    String value= strArr[i++];

                    Cookie ckNew = new Cookie(name, value);

                    Cookie cookie = new Cookie.Builder(name,value)
                    .expiresOn(new Date(2017, 31,12))
                    //Set<Cookie> myCookies =driver.manage().getCookies();
                    //for(Cookie ck:myCookies)
    } catch (IOException e)

  • We need specifics of your site including how the authentication works and is stored. Sometimes it's just a cookie and other times there are various factors in authentication and you may not have all the pieces, but from the description all we can see is you took cookies and then it's not working. No information with the application specifically and the underneath of how it works. – mutt Apr 3 '17 at 17:23
  • It is just a example code I am just trying to store email and password in cookies – gayatri Apr 4 '17 at 5:51
  • Examples are nice for understanding, but to automate authentication the way you are going we need hard specifics to the application under test to be sure the authentication is correct. – mutt Apr 4 '17 at 14:15
  • I am getting exception as Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs. when i tried driver.manage().addCookie(ckNew); – gayatri Apr 11 '17 at 5:46
  • so cookies for either the browser or security cookies are locked to outside access. I'm guessing probably the browser cookies are enabled? That might be a protected security cookie allowing only browser access and not third party access... – mutt Apr 11 '17 at 11:51

In general handling of cookies is not required for functional testing.

Selenium will run each script in new private window. So the cookies are limited to that script or browser window. Once the session is closed then it is not possible to use cookies for next session

Cookies will be used if you are navigating to different pages from same browser window.

So perform all operations in one script then no need to handle cookies.

  • can you explain "In general handling of cookies is not required for functional testing." ? – Rsf Apr 7 '17 at 9:17
  • In functional test browser is opened, so it will handle when we navigate to other pages. Where as in performance testing the browser won't open so we need to handle cookies on our own – Karthikeya Apr 7 '17 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.