21

Selenium WebDriver manager().getCookies() in InternetExplorerDriver always returns 0 elements! P.S. version 2.32.0.0

Serhii Bohutskyi
  • 2,261
  • 2
  • 28
  • 28

7 Answers7

15

If the cookies are HTTPOnly you can't read them from Javascript/Selenium

Eitan Peer
  • 4,335
  • 27
  • 29
5

I didn't understand why driver.manage().getCookies(); always returns with size 0 in FF and IE.

but I found this workaround, using executeScript

Set<Cookie> cookies = driver.manage().getCookies();

if (cookies.size() == 0) { // To support FF and IE
    String cookiesString = (String) driver.executeScript("return document.cookie");
    cookies = parseBrowserCookies(cookiesString);
}




private Set<Cookie> parseBrowserCookies(String cookiesString) {
    Set<Cookie> cookies = new HashSet<>();

    if (StringUtils.isBlank(cookiesString)) {
        return cookies;
    }

    Arrays.asList(cookiesString.split("; ")).forEach(cookie -> {
        String[] splitCookie = cookie.split("=", 2);
        cookies.add(new Cookie(splitCookie[0], splitCookie[1], "/"));
    });

    return cookies;
}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Elhay Avichzer
  • 836
  • 1
  • 10
  • 14
  • 1
    //Replace driver.executeScript with js.executeScript like: JavascriptExecutor js; js = (JavascriptExecutor)driver; String cookiesString = (String) js.executeScript("return document.cookie"); – Viorel Mirea Feb 18 '22 at 15:02
3

Are you sure your webdriver is on the domain you are expecting the cookie to be set on? getCookies only returns the cookies for the current domain.

stracktracer
  • 1,862
  • 5
  • 24
  • 37
3
Set<Cookie> allcookies = driver.manage().getCookies();
System.out.println(allcookies);
Tom
  • 4,257
  • 6
  • 33
  • 49
  • While the code you've provided may answer the question - adding some content to explain why will ensure this is actually a helpful answer. Please review [ask] if you're unsure of how to write an answer. – Tom May 08 '17 at 10:39
  • 1
    The question is not how to get cookies with Selenium, is why `driver.manage().getCookies()` not working in IE. So this is not answering the question. – Elhay Avichzer Mar 11 '19 at 14:32
1

The issue appears to be with the 64-bits driver. I have tried with the 32-bits driver and it works. If using WebDriverManager just do:

WebDriverManager.iedriver().arch32().setup();

Another thing that could be related(not very sure) is a setup for Internet Explorer 11 at registry level, see Selenium IE driver information:

For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

silver_mx
  • 828
  • 9
  • 16
0

Maybe you need to set cookie first, AFAIK webdriver always start with fresh session. Instead you can try to setup user profile like this: How to make FirefoxDriver use existing profile?

Community
  • 1
  • 1
buddy
  • 183
  • 6
  • I'm using IE, and my session cookie are sent with next requests, but I can't get them from webdriver. I think its problem with IE http://stackoverflow.com/questions/15970490/ie10-console-cant-see-cookies-by-command-document-cookie – Serhii Bohutskyi Apr 15 '13 at 08:27
-3

This is what you could do to get all cookies

allCookies = driver.manage().getCookies();
Amey
  • 8,470
  • 9
  • 44
  • 63