13

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.

  • 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? – Peter M. Dec 5 '14 at 16:08
  • @PeterMasiar - to my knowledge it's not true - are you 100% sure that localStorage is cleared? – shabunc Dec 5 '14 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. – Peter M. Dec 5 '14 at 17:15
  • @PeterMasiar thanks, this is actually very important, should double check. – shabunc Dec 5 '14 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. – Steve Saporta Jan 27 '15 at 13:23
7

I found another solution. For me this worked like a charm and it's the prettiest way.

driver.getSessionStorage().clear();
driver.getLocalStorage().clear();
  • If you have the generic WebDriver, you need to check if it support this: if (driver instanceof WebStorage) { ((WebStorage)driver).getSessionStorage().clear(); ...} – Kutzi Mar 26 at 13:51
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();')

3

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();')
1

In c# the current way to do this is

driver.WebStorage.LocalStorage.Clear();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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