Selenium WebDriver Script to clear browser history in Chrome

There will be some applications that requires the browser history to be cleaned before launching it. The below function helps to do the same in Java

Notice that you will have to give a large wait time since you may not know how much of data is there in cache and how much time your system may take to clear it out.

 public void clearChromeBrowsingHistory(){
//set the implicit wait time to 60 secs
sync.setImplicitWait(2);
driver.get("chrome://settings/clearBrowserData");
WebElement clearDataBtn = driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"));
if(sync.isElementPresent(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"))){
System.out.println("Clear button found");
clearDataBtn.click();
}
//int wait=0;
do{
//System.out.println("Waiting for cache to clear:"+wait);
//wait++;
}while(sync.isElementDisplayed(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")));
//set the implicit wait time back to 30 secs
sync.setImplicitWait(30);
}

Leave a comment