Skip to main content

How to Clear Local Storage using Selenium WebDriver?

How to Clear Local Storage using Selenium WebDriver?

Key takeaways

  • To clear local storage in Selenium, run window.localStorage.clear() through JavascriptExecutor. It works with every driver.
  • The page must already be loaded on the origin whose storage you want to clear: local storage belongs to one origin, and the browser refuses it on data: pages.
  • Clearing cookies does not clear local storage, so state survives between tests unless you clear it too.
  • Selenium’s WebStorage and LocalStorage classes were deprecated in 4.17.0 and removed in 4.35.0, so the old ((WebStorage) driver).getLocalStorage() code no longer compiles.

To clear local storage with Selenium WebDriver in Java, cast the driver to JavascriptExecutor and run window.localStorage.clear() while a page from the right origin is open:

((JavascriptExecutor) driver).executeScript("window.localStorage.clear();");

That one line is all most tests need. The rest of this post covers a small helper for the other storage operations, why the API older tutorials show has gone, and where in a test the call belongs.

How do I clear local storage in Selenium WebDriver?

Open a page on the site under test first, then run the script. Local storage is scoped to the origin (scheme, host and port), so http://example.com and https://example.com have separate stores, and a fresh browser sitting on a blank tab has nothing to clear. The MDN page for Window.localStorage describes the scoping and says that a data: origin throws a SecurityError.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClearStorageExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");
            ((JavascriptExecutor) driver).executeScript(
                "window.localStorage.clear(); window.sessionStorage.clear();");
        } finally {
            driver.quit();
        }
    }
}

With Selenium 4.6 or later, new ChromeDriver() needs no manual chromedriver download, because Selenium Manager fetches the driver. The same script works for Firefox, Edge and Safari drivers, since it runs inside the page rather than through a browser-specific command.

Session storage is a separate store with the same API, so window.sessionStorage.clear() clears that one. Cookies are a third store and need driver.manage().deleteAllCookies(). A test that resets only cookies leaves local storage behind, which is a common reason a test passes alone and fails inside a suite.

A small helper for local storage in Selenium tests

If your tests read and write storage often, wrap the calls once. Passing values as script arguments (arguments[0]) avoids the quoting bugs that string concatenation causes when a key or value contains an apostrophe:

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class LocalStorageJS {

    private final JavascriptExecutor js;

    public LocalStorageJS(WebDriver driver) {
        this.js = (JavascriptExecutor) driver;
    }

    public void setItem(String key, String value) {
        js.executeScript("window.localStorage.setItem(arguments[0], arguments[1]);", key, value);
    }

    public String getItem(String key) {
        return (String) js.executeScript("return window.localStorage.getItem(arguments[0]);", key);
    }

    public void removeItem(String key) {
        js.executeScript("window.localStorage.removeItem(arguments[0]);", key);
    }

    public long size() {
        return (Long) js.executeScript("return window.localStorage.length;");
    }

    public void clear() {
        js.executeScript("window.localStorage.clear();");
    }

    public void clearSession() {
        js.executeScript("window.sessionStorage.clear();");
    }
}

getItem returns null when the key does not exist, which is how you test whether an item is present. JavaScript numbers come back from executeScript as Long when they are whole, which is why size() casts to Long.

Why does ((WebStorage) driver).getLocalStorage() not compile anymore?

Older versions of this tutorial, and many answers online, use Selenium’s HTML5 package:

LocalStorage local = ((WebStorage) driver).getLocalStorage();
local.clear();

That API is gone. The Selenium Java changelog lists “Deprecate all html5 offline storage implementations” in 4.17.0 and “Removing deprecated WebStorage related classes” in 4.35.0 (Selenium Java CHANGELOG, checked October 2026). I compiled the examples above against the Selenium 4.51.0 jars from Maven Central: they build, and org.openqa.selenium.html5.WebStorage is not in those jars. If you are stuck on an old Selenium 3 or early 4.x project, the call still works there, but plan to move to the JavaScript route because it is the one that stays supported.

Should I clear local storage in setup or teardown?

Clear it in setup, before the test navigates anywhere that depends on it. A test that crashes never reaches its teardown, so a cleanup that lives only there lets state leak into the next test. In JUnit 5 that is a @BeforeEach method that opens the base URL and clears storage, or a base class every test extends.

Two other ways to start from a clean state:

  • Start a new driver per test. ChromeDriver creates a new temporary profile for each session (ChromeDriver capabilities, checked October 2026), so a fresh new ChromeDriver() begins with empty storage. It costs a browser start per test.
  • Set a known value instead of clearing. When the app needs a flag in local storage to skip a tour or a cookie banner, write it with setItem after loading the page and reload. The test then starts from a defined state rather than an empty one.

Before you chase a storage bug, check the basics: that the element you expect is found (locate web elements with Selenium WebDriver), and that the script runs after the page has loaded (explicit and implicit waits). If a test opens a second window, storage for the same origin is shared with it (open a new tab with Selenium).

Once a Selenium suite is stable, the same pages can be put under traffic. LoadFocus runs cloud load tests against a site or API, which tells you something Selenium cannot: how the app behaves with many users at once.

Frequently Asked Questions

Does clearing cookies also clear local storage?

No. Cookies, local storage and session storage are separate stores, so state written to local storage survives deleteAllCookies() and leaks into the next test. This is a common cause of tests that only fail when run as part of a suite.

How do I clear local storage in Selenium?

Cast the driver to JavascriptExecutor and run window.localStorage.clear() on a page from the origin you want to clear. This works with every driver. Selenium 4.35 and later no longer ship the WebStorage classes, so there is no dedicated Java API for it.

Why do I get a SecurityError when clearing local storage?

The page is not on an origin that has storage. Browsers refuse local storage on data: pages, and Chrome also refuses it when the user or profile blocks site data. Navigate to the real site with driver.get(...) first, then run the script.

How do I clear session storage in Selenium?

Run window.sessionStorage.clear() the same way. Session storage lasts only for the life of the tab, so a new driver or a new tab starts empty, but clearing it explicitly in a long test is harmless.

Should I clear it in setup or teardown?

Setup. A test that crashes never reaches its teardown, so cleaning up beforehand is the only version that holds when something goes wrong.

Sources

Chris

Head of Content at LoadFocus

Chris leads content and oversees development across the services this blog covers. The guides and tool comparisons here come out of the same decisions that shape what ships.

Weekly tips right in your inbox.

Don’t miss out on the latest testing tips and tricks, tools and news related to software testing.

Already +20,000 members in the list.

The Load Testing Platform with AI That Explains Your Results

Load test websites and APIs from 25 cloud regions, monitor page speed and uptime, and get AI analysis that explains your results in plain English.

Cloud load testing, website speed and API monitoring in one place.

Hi there! Can I share a cool thing I'm working on with you?

A software testing newsletter that sparks joy.

Our goal is to create helpful content for software developers and testers.

If you're not into it, you can unsubscribe instantly.