Clear IE Cache of Selenium Web Driver

Home/Selenium/Clear IE Cache of Selenium Web Driver

Clear IE Cache of Selenium Web Driver

Usually you want a fresh session without old cashing data and without any old cookies when you start a new run of your Selenium tests. For example if you are testing an online shop an in your last test you have put some products in your basket, you probably don’t want when you start your next test a pop up showing up that asks you “You still have products in your basket, do you want to keep them there?”

The Chrome and Firefox WebDriver already starts with a clear session by default when creating a new instance. Unfortunately the Internet Explorer WebDriver doesn’t.

You can force the IE Web Driver though to start with a clear session with empty cache and no cookies stored. To do this you need to initialize the IE WebDriver instance using DesiredCapabilities like this:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

WebDriver iEWebDriver = new InternetExplorerDriver(capabilities);

The most simple Selenium program using those DesiredCapabilites, would then look like this:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class RunTest {
	public static void main(String[] args) {
		System.setProperty(
				"webdriver.ie.driver",
				"C:/Users/Nils/Programm Daten/Eclipse Libs/WebDriver/IEDriverServer_x64_2.44.0/IEDriverServer.exe");

		// Setting IE to clean session
		DesiredCapabilities capabilities = DesiredCapabilities
				.internetExplorer();
		capabilities.setCapability(
				InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

		WebDriver iEWebDriver = new InternetExplorerDriver(capabilities);
		iEWebDriver.navigate().to("https://www.google.com");
		iEWebDriver.close();
		iEWebDriver.quit();

	}
}
By |August 17th, 2015|Selenium|1 Comment

One Comment

  1. Stroev December 6, 2016 at 2:23 pm - Reply

    Thanks a lot. This is extremely helpful information and it did work for me 🙂

Leave A Comment