Searching for “Clear Data” in the Inspector reveals that the first button we need to click is identified by id #clearSiteDataButton.Ĭlicking a button in Selenium is pretty easy. While right-clicks are not available and we cannot examine the target buttons directly, we can pull up Firefox’s developer tools and locate the elements we need to interact with in the Inspector tab.
# Accept the "Clear Data" dialog by clicking on the "Clear" button.ĭriver.execute_script(accept_dialog_script)įirst up, let’s talk about the “Clear Data” button that we need to start with. Get_clear_site_data_button(driver).click() # Click the "Clear Data." button under "Cookies and Site Data". Return driver.find_element_by_css_selector('#clearButton')ĭef clear_firefox_cache(driver, timeout=10): Return driver.find_element_by_css_selector(dialog_selector)ĭef get_clear_site_data_confirmation_button(driver): Return driver.find_element_by_css_selector('#clearSiteDataButton') 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.
#How do i enable java in firefox 57 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.
#How do i enable java in firefox 57 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.