How can I delete google chrome cookies with selenium?

17,269

Solution 1

The method DeleteAllCookies(); will delete all the cookies for the current domain [Handling Cookies in Webdriver].

If you want to delete all cookies for all domain you should use solution from Chrome settings "Clear browsing data".

Here you have example of usage.

Solution 2

Chrome supports DevTools Protocol commands like Network.clearBrowserCookies that you can call remotely that will delete cookies for all domains. Selenium does not support this because it's not part of the standard and it doesn't work in other browsers than Chrome.

However, you can add support for these commands by patching Selenium's supported commands like this:

send_command = ('POST', '/session/$sessionId/chromium/send_command')
driver.command_executor._commands['SEND_COMMAND'] = send_command

Now you can call any DevTools Protocol command like

driver.execute('SEND_COMMAND', dict(cmd='Network.clearBrowserCookies', params={}))

This deletes all cookies for all domains.

Share:
17,269
JayGatsby
Author by

JayGatsby

Updated on June 04, 2022

Comments

  • JayGatsby
    JayGatsby almost 2 years

    I'm trying delete all google chrome cookies. I'm using this code which I found it on selenium's website:

    var driver = new ChromeDriver();
    driver.Manage().Cookies.DeleteAllCookies();
    

    But when I execute it a dos page appears with this text

    Starting ChromeDriver 2.13.307647 (5a7d0541ebc58e69994a6fb2ed930f45261f3c29) on
    port 25398
    Only local connections are allowed.
    

    And google chrome starting. What did I miss? How can I delete stored cookies?