21

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

3
  • 1
    Can you give some code examples? Apr 11, 2013 at 15:05
  • Are you sure visbile cookies exist? What is displayed if you run "document.cookie" in the JavaScript console?
    – Ardesco
    Apr 12, 2013 at 11:46
  • I dig deeply and find out that its problem in IE10, it doesn't show cookies at all ( I try in console by command 'document.cookie'). Apr 12, 2013 at 11:57

7 Answers 7

15

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

2
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;
}
1
  • 1
    //Replace driver.executeScript with js.executeScript like: JavascriptExecutor js; js = (JavascriptExecutor)driver; String cookiesString = (String) js.executeScript("return document.cookie"); Feb 18, 2022 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.

3
Set<Cookie> allcookies = driver.manage().getCookies();
System.out.println(allcookies);
2
  • 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 How to Ask if you're unsure of how to write an answer.
    – Tom
    May 8, 2017 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. Mar 11, 2019 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.

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?

1
-3

This is what you could do to get all cookies

allCookies = driver.manage().getCookies();
1
  • 3
    I know that. The problem is in IE or in driver, I can't get cookies at all! Cookies exists, but driver returns 0... Apr 12, 2013 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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