Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from html 5 local storage with selenium

I am using selenium web driver in c#. when i write to local storage with JavaScript method it works fine ( i can see the values in the html page with inspect ), but if i try to read from local storage what i just wrote it returns Null. Thanks for any help .

Writing (works):

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;      
js.ExecuteScript("localStorage.setItem('key','value');");  

Reading (returns Null ):

Object Value= js.ExecuteScript("localStorage.getItem('key')");
like image 812
user3553435 Avatar asked Apr 20 '14 08:04

user3553435


People also ask

Can we use localStorage in HTML?

localStorage as a type of web storage is an HTML5 specification. It is supported by major browsers including IE8. To be sure the browser supports localStorage , you can check using the following snippet: if (typeof(Storage) !==


2 Answers

First of all, you should return the value, secondly you should cast the return value to a known type.

value = (String) js.executeScript("return localStorage.getItem('key')");
like image 193
Erki M. Avatar answered Oct 08 '22 13:10

Erki M.


Update to the question, in 2018 with c# you would do:

browser.WebStorage.LocalStorage.GetItem("item")
like image 4
scott Avatar answered Oct 08 '22 14:10

scott