Sometimes the fresh instance of Chrome will need to start with a clean browser history, cookies, and cache while working with selenium in python. I found this method working 100% with updated chrome browser.
The magic function
Function to clear chrome browser cache:
1 2 3 4 5 6 7 8 9 10 | def clear_cache(browser): """Clear the cookies and cache for the ChromeDriver instance.""" try: # navigate to the settings page browser.get('chrome://settings/clearBrowserData') browser.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER) except Exception as ex: print(str(ex)) browser.quit() |
This clear_cache(driver) method will clear the browsing data in the same way that a user would. If you execute it, by using
How to use?
Just create python file or add in existing file as per your requirements:
1 2 3 4 5 | from selenium import webdriver browser = webdriver.Chrome() clear_cache(browser) |
Thus, you will be able to clear browsing history, cookies and other stuff of chrome browser.