Topic: https://intoli.com/blog/clear-the-firefox-browser-cache/
hide preview

What's next? verify your email address for reply notifications!

PDMQDGzC 6y, 96d ago [edited]

how to click on clear button to delete the cookies and caches in firefox 61.0.1 (64-bit) with selenium.

Once I enter the url "about:preferences#privacy". I am navigated to Options page. Then clicked on "Clear data" button under cookies and site data. It got successful. Now the pop up appears stating that "Clearing all cookies and site data stored by Firefox may sign you out of websites and remove offline web content. Clearing cache data will not affect your logins." Then I need to click on Clear button which is not getting automated. Then One alert box appears. I should click on that accept button also. Please help me how to automate the clear button first...

remark link
hide preview

What's next? verify your email address for reply notifications!

andre 6y, 73d ago [edited]

Thanks for pointing this out. I updated the script and article for FF 61.

hide preview

What's next? verify your email address for reply notifications!

unverified 5y, 73d ago [edited]

I translated your code in Java, in case someone needs it. Tested with selenium-java 3.141.59 and Firefox 68:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;

public class FirefoxClearCache {

    private final String dialogSelector = "#dialogOverlay-0 > groupbox:nth-child(1) > browser:nth-child(2)";

    private final String acceptDialogScript = "const browser = document.querySelector('" + dialogSelector + "');"
            + "browser.contentDocument.documentElement.querySelector('#clearButton').click();";

    private By getClearSiteDataButton() {
        return By.cssSelector("#clearSiteDataButton");
    }

    private By getClearSiteDataDialog() {
        return By.cssSelector(dialogSelector);
    }

    private void clearFirefoxCache() {

        driver.get("about:preferences#privacy");
        WebDriverWait wait = new WebDriverWait(driver, 10);

        // Click the "Clear Data..." button under "Cookies and Site Data".
        wait.until(ExpectedConditions.visibilityOfElementLocated(getClearSiteDataButton()));
        driver.findElement(getClearSiteDataButton()).click();

        // Accept the "Clear Data" dialog by clicking on the "Clear" button.
        wait.until(ExpectedConditions.visibilityOfElementLocated(getClearSiteDataDialog()));
        ((JavascriptExecutor) driver).executeScript(acceptDialogScript);

        // Accept the confirmation alert.
        wait.until(ExpectedConditions.alertIsPresent());
        driver.switchTo().alert().accept();

    }
}
remark link
hide preview

What's next? verify your email address for reply notifications!

evan 4y, 343d ago

Thanks!

hide preview

What's next? verify your email address for reply notifications!