title: Webdriver get Item From Local Storage
body: <p>I'm trying to get an item from local storage using Selenium Webdriver.</p>
  
  <p>I followed <a href="https://gist.github.com/roydekleijn/5073579" rel="noreferrer">this site</a> but when I run my code I get <code>NullPointerException</code>.</p>
  
  <p>When I debug the code I see the function: getItemFromLocalStorage returns NULL for some reason.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class storage
   {
      public static WebDriver driver;
      public static JavascriptExecutor js;
  
      public static void main(String[] args)
      {       
          System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
          driver = new ChromeDriver();
          driver.get("http://html5demos.com/storage");
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.findElement(By.id("local")).sendKeys("myLocal");
          driver.findElement(By.id("session")).sendKeys("mySession");
          driver.findElement(By.tagName("code")).click(); // just to escape textbox
  
          String sItem = getItemFromLocalStorage("value");
          System.out.println(sItem);
      }
  
      public static String getItemFromLocalStorage(String key)
      {
          return (String) js.executeScript(String.format(
              "return window.localStorage.getItem('%s');", key));
      }
  }
  </code></pre>