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. - stands for Monica– Peter M. - stands for Monica2014年12月05日 16:08:25 +00:00Commented Dec 5, 2014 at 16:08
-
@PeterMasiar - to my knowledge it's not true - are you 100% sure that localStorage is cleared?shabunc– shabunc2014年12月05日 16:45:39 +00:00Commented Dec 5, 2014 at 16:45
-
3If 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. - stands for Monica– Peter M. - stands for Monica2014年12月05日 17:15:26 +00:00Commented Dec 5, 2014 at 17:15
-
@PeterMasiar thanks, this is actually very important, should double check.shabunc– shabunc2014年12月05日 19:31:31 +00:00Commented 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.Steve Saporta– Steve Saporta2015年01月27日 13:23:56 +00:00Commented Jan 27, 2015 at 13:23
4 Answers 4
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();
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();')
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();')
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();");
}
Explore related questions
See similar questions with these tags.