Deleting Browser Cache with Python Selenium and Chromedriver

Picture by Selenium

Quick and dirty

After googling quite a bit I couldn’t find a satisfying out-of-the-box solution for cache clearing. I came up with a quick and dirty solution which you can find on stackoverflow. Simply open a new tab, go to Chrome’s settings, press the right key combo, wait, close the tab and go back. I tested the script in November 2020.

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/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()

Just beware that Chromium saves the last user settings. Hence only the first time you should uncheck “Cookies” so your login session doesn’t get deleted. Simply add a default argument to the function and we are good to go.

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(first=False):
     
    driver.execute_script("window.open('');")
    time.sleep(2)
    
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(2)
    
    driver.get('chrome://settings/cleardriverData')
    time.sleep(2)
    
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 2) # send right combination
    actions.perform()
    time.sleep(1)
    
    if first == True:
        actions = ActionChains(driver) 
        actions.send_keys(Keys.DOWN * 4) # send right combination
        actions.perform() # theoretically you don't need to add the condition here as the last option is always "everything"
        time.sleep(1) # additional "down" clicks won't change anything, anyway let's keep it clean 
    
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 2) # send right combination
    actions.perform()
    time.sleep(1)
    
    if first == True: # only first time, chrome saves input from first time
        actions = ActionChains(driver) 
        actions.send_keys(Keys.SPACE) # send right combination
        actions.perform()
        time.sleep(1)
    
    actions = ActionChains(driver) 
    actions.send_keys(Keys.TAB * 3 + Keys.ENTER) # confirm
    actions.perform()
    time.sleep(15) # wait some time to finish
    
    driver.close() # close this tab
    driver.switch_to.window(driver.window_handles[0]) # switch back
Share:

Use the left and right arrow keys to change images. Press Escape to close.

Back to Blog