Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Clear chrome cache

In my application I need a way to clear only the cache of the chrome browser before log out (except cookies - I do not want to delete cookies).

Can any one suggest me a way to click on the CLEAR DATA button in chrome. I have written the below code but the code is not working.

Configuration :

Chrome Version: Version 65.0.3325.181 (Official Build) (64-bit)

Selenium Version: 3.11.0

//Clear the cache for the ChromeDriver instance.
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(10000);
driver.findElement(By.xpath("//*[@id='clearBrowsingDataConfirm']")).click();

enter image description here

like image 860
VISHVAMBRUTHJAVAGALTHIMMEGOWDA Avatar asked Apr 02 '18 15:04

VISHVAMBRUTHJAVAGALTHIMMEGOWDA


People also ask

Does selenium clear cache?

Selenium does not provide a way to delete cache, here are some workarounds. There are a few ways in which cache can be deleted using Selenium Webdriver for regular Chrome browsers. For instance, here they explain how selenium can be used to clear cache like a real user i.e., by navigating to chrome settings.

How we can clear the cookie in selenium?

Navigate to the chrome settings page with Selenium by executing the driver. get('chrome://settings/clearBrowserData') . Click on the Clear Data button to clear the cache.


4 Answers

You are using here

driver.findElement(By.xpath("//*[@id='clearBrowsingDataConfirm']")).click();

Unfortunately, this won’t work because the Chrome settings page uses Polymer and WebComponents, need to use query selector using the /deep/ combinator, so selector in this case is * /deep/ #clearBrowsingDataConfirm.

Here is workaround to your problem...you can achieve the same using either one of the following...

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class ClearChromeCache {

    WebDriver driver;

    /*This will clear cache*/
    @Test
    public void clearCache() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("disable-infobars");
        chromeOptions.addArguments("start-maximized");
        driver = new ChromeDriver(chromeOptions);
        driver.get("chrome://settings/clearBrowserData");
        Thread.sleep(5000);
        driver.switchTo().activeElement();
        driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
        Thread.sleep(5000);
    }

    /*This will launch browser with cache disabled*/
    @Test
    public void launchWithoutCache() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability("applicationCacheEnabled", false);
        driver = new ChromeDriver(cap);
    }
}
like image 80
Sodium Avatar answered Sep 21 '22 07:09

Sodium


YEAR 2020 Solution (using Selenium 4 alpha):

Using the devtools

    private void clearDriverCache(ChromeDriver driver) {
    driver.getDevTools().createSessionIfThereIsNotOne();
    driver.getDevTools().send(Network.clearBrowserCookies());
    // you could also use                
    // driver.getDevTools().send(Network.clearBrowserCache());
}
like image 39
Soufiane Azeroual Avatar answered Sep 21 '22 07:09

Soufiane Azeroual


Chrome supports DevTools Protocol commands like Network.clearBrowserCache (documentation). Selenium does not have an interface for this proprietary protocol by default.

You can add support by expanding Selenium's commands:

driver.command_executor._commands['SEND_COMMAND'] = (
    'POST', '/session/$sessionId/chromium/send_command'
)

This is how you use it:

driver.execute('SEND_COMMAND', dict(cmd='Network.clearBrowserCache', params={}))

Note: this example is for Selenium for Python, but it's also possible in Selenium for other platforms in a similar way by expanding the commands.

like image 36
Blaise Avatar answered Sep 21 '22 07:09

Blaise


Don´t forget the send keys!!!!

For Selenium Basic, below code is functional.

Function clean_cache()

    Set driver = New ChromeDriver
    Dim keys As New Selenium.keys


                driver.Get "chrome://settings/clearBrowserData"
                Sleep 5000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Tab)
                Sleep 1000
                driver.SendKeys (keys.Enter)
                Sleep 2000
                driver.Quit

End Function
like image 28
Denis Rodrigues Avatar answered Sep 18 '22 07:09

Denis Rodrigues