19

I want to call localStorage.clear() before running a group of specific tests but the thing is I don't know how. I can see CLEAR_LOCAL_STORAGE variable mentioned here but still failing to understand where exactly this constant is used and how I can perform desired action.

If calling js execution is the only option I'm OK with it but the existence of this constant makes me looking for a better solution.

5
  • Why you feel you need to clear LocalStore? Selenium will give you new instance of browser with nothing in store or anywhere. Is it business requirement or just your hunch? Commented Dec 5, 2014 at 16:08
  • @PeterMasiar - to my knowledge it's not true - are you 100% sure that localStorage is cleared?
    – shabunc
    Commented Dec 5, 2014 at 16:45
  • 3
    If you get new instance of the browser, it should get new local storage, IIUC. No cache. It took 40 secs to build new instance of Firefox until recently. I haven't checked localStore. Commented Dec 5, 2014 at 17:15
  • @PeterMasiar thanks, this is actually very important, should double check.
    – shabunc
    Commented Dec 5, 2014 at 19:31
  • I have confirmed by testing both the Firefox and Chrome drivers, that upon instantiating the driver, the browser's localStorage is empty. This is true even when another instance of the same browser is already open in another window. My environment is Ubuntu 14.04, Python 2.7.6, Selenium 2.44.0. Commented Jan 27, 2015 at 13:23

4 Answers 4

12

If your driver implements WebStorage you can invoke

driver.getSessionStorage().clear();
driver.getLocalStorage().clear();

In order to make sure that the driver does implement WebStorage the idiom is

if (driver instanceof WebStorage) {
    WebStorage webStorage = (WebStorage)driver;
    webStorage.getSessionStorage().clear();
    webStorage.getLocalStorage().clear();
}

or better

if (!(driver instanceof WebStorage)) {
    throw new IllegalArgumentException("This test expects the driver to implement WebStorage");
}
WebStorage webStorage = (WebStorage)driver;
webStorage.getSessionStorage().clear();
webStorage.getLocalStorage().clear();
0
6

The answer Peter Masiar gave no longer works. This answer will result in the following exception:

Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in get self.execute(Command.GET, {'url': url}) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: unsupported protocol (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.3 x86_64)

In order to clear the localStorage the execute_script() method from WebDriver should be used.

WebDriver.execute_script('window.localStorage.clear();')
3

After you driver.get(URL) to your page, you can execute javascript in it from webdriver, like this

driver.get('javascript:<your JS snippet here>')

so try

driver.get('javascript:localStorage.clear();')

1

In C#, the current way to do this is:

if (driver.HasWebStorage) {
   driver.WebStorage.LocalStorage.Clear();
}
else {
   // you're out of luck...
}

Update (Selenium 4)

In Selenium 4, HasWebStorage and WebStorage are marked as obsolete:

[CS0618] 'WebDriver.HasWebStorage' is obsolete: 'Use JavaScript instead
for managing localStorage and sessionStorage. This property will be removed 
in a future release.'

It looks like the intended way is now:

if ((bool?)driver.ExecuteScript("return !!window.localStorage") == true) {
    driver.ExecuteScript("window.localStorage.clear();");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.