5

How can I clear the cache of a new instance of a chromedriver in java? I am trying this but im not too sure what else to do? Would it be possible to create a javascript hack to clear the cache in JS which I can call from my driver?

private static WebDriver makeDriver() {
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    ChromeDriver driver = new ChromeDriver();
    driver.manage().deleteAllCookies();
    return driver;
}
Ben
  • 391
  • 1
  • 5
  • 19
  • By default, a completely clean profile with an empty cache is fired up by selenium. Your local storage and cookies will also be crystal clear. Could you please elaborate on what your actual problem is you are trying to solve? – alecxe Nov 04 '15 at 18:43
  • @alecxe not too sure, as I am trying to go to a website and I can't access it because I have previously logged in... I just want to be safe and make sure its clear. – Ben Nov 04 '15 at 18:44
  • Thanks, is this a private or a public website? In other words, can you make the problem reproducible for us? – alecxe Nov 04 '15 at 18:46
  • @alecxe sorry private internal website... so no sadly. – Ben Nov 04 '15 at 18:46
  • Okay, no problem, do you see the same behavior in Firefox? – alecxe Nov 04 '15 at 18:47
  • @alecxe yes sadly. I am about to try IE. But is it possible to create a batch file to call to clear it before I navigate to the site? – Ben Nov 04 '15 at 18:48

1 Answers1

9

By default, a completely clean profile with an empty cache, local storage, cookies is fired up by selenium. You are actually browsing privately with selenium.

First of all, there is a problem in your code - you are not passing your DesiredCapabilities instance to the webdriver constructor (not tested though):

ChromeDriver driver = new ChromeDriver(capabilities);

You may also try forcing the "incognito" mode:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));

ChromeDriver driver = new ChromeDriver(capabilities);
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195