How to clear session storage using selenium c#?

Viewed 1481

I have already tried this, but it shows error while debugging

        IWebStorage webStorage = (IWebStorage)driver;
        webStorage.LocalStorage.Clear();
        webStorage.SessionStorage.Clear();

error: "Unable to cast object of type 'OpenQA.Selenium.Chrome.ChromeDriver' to type 'OpenQA.Selenium.Html5.IWebStorage"

1 Answers

You can clear local storage and session storage with javaScript executor.

(driver as IJavaScriptExecutor).ExecuteScript("sessionStorage.clear();");
(driver as IJavaScriptExecutor).ExecuteScript("localStorage.clear();");

Just keep in mind that session and local storage are stored specific to a page so you have to load the url of the page you want to clear before executing the script.

Related