Serhii Bohutskyi
Serhii Bohutskyi

Reputation: 2259

Selenium WebDriver manager().getCookies() returns 0 always

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

Upvotes: 21

Views: 45054

Answers (7)

Elhay Avichzer
Elhay Avichzer

Reputation: 856

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;
}

Upvotes: 5

silver_mx
silver_mx

Reputation: 1382

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.

Upvotes: 1

sidharth shukla
sidharth shukla

Reputation: 55

Set<Cookie> allcookies = driver.manage().getCookies();
System.out.println(allcookies);

Upvotes: 3

stracktracer
stracktracer

Reputation: 1900

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.

Upvotes: 3

Eitan Peer
Eitan Peer

Reputation: 4345

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

Upvotes: 15

buddy
buddy

Reputation: 183

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?

Upvotes: 0

Amey
Amey

Reputation: 8548

This is what you could do to get all cookies

allCookies = driver.manage().getCookies();

Upvotes: -3

Related Questions