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

- 2,254
- 2
- 12
- 24
-
It is possible to clear cache using this post's answer further down that suggests using sendkeys https://stackoverflow.com/questions/49614217/selenium-clear-chrome-cache – Jun 23 '20 at 14:33
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.

- 1,600
- 1
- 10
- 16
-
`chrome://settings/cleardriverData` just lands on `chrome://settings` for me. – Marcel Wilson Jan 12 '21 at 19:32
-
Might be renamed. Can't test right now, but does `chrome://settings/clearBrowserData` work? Otherwise just go to 'chrome://settings` and click on the respective section. Check the URL and change `cleardriverData` to the new section name. – do-me Jan 13 '21 at 18:34
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()

- 3,307
- 4
- 18
- 34
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")

- 1,791
- 20
- 14
-
this is not that simple, the pypi `keyboard` require OS access and it's not trivial in all env and dockers – confiq Jun 13 '22 at 10:56
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

- 3,829
- 11
- 26
- 32

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

- 56
- 4
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 fast\slow
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

- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Blue Robin Feb 28 '23 at 00:46