Python/Selenium – Clear the cache and cookies in my chrome webdriver?

Question:

I’m trying to clear the cache and cookies in my chrome browser (webdriver from selenium) but I can’t find any solutions for specifically the chrome driver. How do I clear the cache and cookies in Python? Thanks!

Asked By: Emmanuel Shaakov

||

Answers:

Taken from this post:

For cookies, you can use the delete_all_cookies function:

driver.delete_all_cookies()

For cache, there isn’t a direct way to do this through Selenium. If you are trying to make sure everything is cleared at the beginning of starting a Chrome driver, or when you are done, then you don’t need to do anything. Every time you initialize a webdriver, it is a brand new instance with no cache, cookies, or history. Every time you terminate the driver, all these are cleared.

Answered By: R.F. Nelson

in step one =>

pip install keyboard

step2 : use it in your code =>

from time import sleep
self.driver.get('chrome://settings/clearBrowserData')
sleep(10)
keyboard.send("Enter")
Answered By: mamal

Cache clearing for Chromedriver with Selenium in November 2020:

Use this function which opens a new tab, choses to delete everything, confirms and goes back to previously active tab.

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome("path/to/chromedriver.exe")

def delete_cache():
    driver.execute_script("window.open('');")
    time.sleep(2)
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(2)
    driver.get('chrome://settings/clearBrowserData') # for old chromedriver versions use cleardriverData
    time.sleep(2)
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 3 + Keys.DOWN * 3) # send right combination
    actions.perform()
    time.sleep(2)
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 4 + Keys.ENTER) # confirm
    actions.perform()
    time.sleep(5) # wait some time to finish
    driver.close() # close this tab
    driver.switch_to.window(driver.window_handles[0]) # switch back
delete_cache()

UPDATE 01/2021: Apparently the settings section in chromedriver is subject to change. The old version was chrome://settings/cleardriverData. In any doubt, go to chrome://settings/, click on the browser data/cache clearing section and copy the new term.

Answered By: do-me

2022 Method That Works

I used a similar method to @do-me’s answer but made it a bit more functional. Also, his Tabs weren’t mapping to the right places for me so I made some edits for it to work in 2022 (on mine at least).

import time
from pathlib import Path

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

CHROMEDRIVER = Path('chromedriver.exe')


def start_driver():
    driver = webdriver.Chrome(executable_path=str(CHROMEDRIVER))
    delete_cache(driver)
    return driver


def delete_cache(driver):
    driver.execute_script("window.open('')")  # Create a separate tab than the main one
    driver.switch_to.window(driver.window_handles[-1])  # Switch window to the second tab
    driver.get('chrome://settings/clearBrowserData')  # Open your chrome settings.
    perform_actions(driver, Keys.TAB * 2 + Keys.DOWN * 4 + Keys.TAB * 5 + Keys.ENTER)  # Tab to the time select and key down to say "All Time" then go to the Confirm button and press Enter
    driver.close()  # Close that window
    driver.switch_to.window(driver.window_handles[0])  # Switch Selenium controls to the original tab to continue normal functionality.


def perform_actions(driver, keys):
    actions = ActionChains(driver)
    actions.send_keys(keys)
    time.sleep(2)
    print('Performing Actions!')
    actions.perform()


if __name__ == '__main__':
    driver = start_driver()
Answered By: Zack Plauché
self.driver.execute_cdp_command('Storage.clearDataForOrigin', {
    "origin": '*',
    "storageTypes": 'all',
})

Here is a solution from me, it uses the chrome devtools protocol to simulate the "Clear all data" button from the application Tab in devtools. I hope it was helpful

Answered By: Zain

None of these worked for me or seemed overly complicated. After some more searching I came across this solution and it works really well and is really simple:

driver = webdriver.Chrome()

driver.execute_cdp_cmd('Storage.clearDataForOrigin', {
    "origin": '*',
    "storageTypes": 'all',
})

This is clearing all cookies, session storage, local storage, and service workers on that instance of chrome(and maybe some more stuff). Quick, lightweight and simple. I am using python 3.9.12 with Selenium 4.1.5.

Answered By: Isaac M
def perform_actions(driver, keys):
    for i in range(0, len(keys)):
        actions = ActionChains(driver)
        actions.send_keys(keys[i])
        sleep(.3) #  adjust this if its going to fastslow
        actions.perform()
    print("Actions performed!")

def delete_cache(driver):
    driver.execute_script("window.open('')")  # Create a separate tab than the main one
    driver.switch_to.window(driver.window_handles[-1])  # Switch window to the second tab
    driver.get('chrome://settings/clearBrowserData')  # Open your chrome settings.
    perform_actions(driver,Keys.TAB * 3 + Keys.LEFT + Keys.TAB * 6 + Keys.ENTER + Keys.TAB + Keys.ENTER + Keys.TAB + Keys.ENTER + Keys.TAB + Keys.ENTER + Keys.TAB * 2 + Keys.ENTER)  # Tab to the time select and key down to say "All Time" then go to the Confirm button and press Enter
    driver.close()  # Close that window
    driver.switch_to.window(driver.window_handles[0])  # Switch Selenium controls to the original tab to continue normal functionality.

edit of @zack-plauché code,
this deletes cache in advanced mode

Answered By: Andh