

I’ll first show you the completed script, and then talk through it piece by piece: from import Alertįrom import expected_conditions as ECįrom import WebDriverWaitĭialog_selector = '#dialogOverlay-0 > groupbox:nth-child(1) > browser:nth-child(2)'į"const browser = document.querySelector('') " +

The Preferences page is coded in Mozilla’s XML-based XUL interface-building language.įortunately, XUL is similar enough to HTML that both Selenium and JavaScript can interact with elements on the page.
Clear browsing data firefox how to#
Now that we know what needs to be done, let’s examine the page in detail and figure out how to execute each step. Since both “Cookies and Site Data” and Cached Web Content” are preselected in this dialog, all our script has to do here is to click the “Clear” button.Ĭlicking the “Clear” button will launch an alert, which the script will have to accept in order to successfully clear the cache, cookies, and site data. This will open an in-page dialog listing the types of data to be cleared. Our script first needs to click the “Clear Data…” button which is highlighted in red in the screenshot below. Let’s first review the steps that the cache clearing script will need to perform, and then describe how to actually go about implementing them with Selenium. The options we care about are in the Browser Privacy section of the Preferences page which you can access by visiting about:preferences#privacy in Firefox. We’ll clear the cache by emulating how a human would accomplish the task: by visiting Firefox’s preferences page and going through the appropriate UI interactions. The cache clearing process is quite similar for older versions of Firefox, and the slightly different cache clearing script written for Firefox 57 is still available in our article materials repository. Update: These instructions have been updated to work with Firefox 61, released on June 26, 2018. The technique is quite similar to the one used to clear the Chrome browser cache, also published on our blog, so head on over there if you prefer using Chrome with Selenium. The rest of the article will describe what this utility actually does.
Clear browsing data firefox code#
If all you’re interested in is the end-result, take a look at the finished utility in Intoli’s article code repository. The code will be written in Python, but you should be able to adapt it to other languages without much difficulty.

In this article I’ll describe how to clear the Firefox browser cache with Selenium. However, lingering cached resources can cause your tests to pass when they shouldn’t, prevent your scrapers from quickly starting clean sessions on demand, and cause all sorts of undesirable behavior besides. This is not particularly surprising given that the WebDriver specification that Selenium uses behind the scenes has no provision for clearing the cache. If you use Selenium for automated testing or web scraping, you may have discovered that there is no built-in utility for clearing browser resources like cookies, cached scripts, and objects in local storage.
