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

- 2,261
- 2
- 28
- 28
-
1Can you give some code examples? – Marthyn Olthof Apr 11 '13 at 15:05
-
Are you sure visbile cookies exist? What is displayed if you run "document.cookie" in the JavaScript console? – Ardesco Apr 12 '13 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'). – Serhii Bohutskyi Apr 12 '13 at 11:57
7 Answers
If the cookies are HTTPOnly you can't read them from Javascript/Selenium

- 4,335
- 27
- 29
-
You can see this in the Chrome Cookies spec: https://developer.chrome.com/extensions/cookies – ShaBANG Oct 03 '16 at 22:05
-
At the moment you can read httponly with selenium. But, of cause, you cannot do that with Javascript. – Oleksandr Kulychok Nov 15 '22 at 17:42
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;
}

- 5,179
- 10
- 35
- 56

- 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
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.

- 1,862
- 5
- 24
- 37
Set<Cookie> allcookies = driver.manage().getCookies();
System.out.println(allcookies);

- 4,257
- 6
- 33
- 49

- 55
- 1
-
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
-
1The 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
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.

- 828
- 9
- 16
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?
-
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
This is what you could do to get all cookies
allCookies = driver.manage().getCookies();

- 8,470
- 9
- 44
- 63
-
3I know that. The problem is in IE or in driver, I can't get cookies at all! Cookies exists, but driver returns 0... – Serhii Bohutskyi Apr 12 '13 at 08:27