Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalStorage in Selenium Webdriver

Are there different LocalStorage stores for normal Browser and Selenium Browser? When i create an item on selenium chrome, after i close the browser the item is gone. Is this intended? Also i can't read the localStorage from the normal Browser

Edit: To be more specific:

If i enter in the console on my Selenium chrome browser

localStorage.setItem("test", "This is a test value"); localStorage.getItem("test"); => prints "This is a test value" as intended

But if i close the Selenium chrome and reopen it and try to get the same value from the same page localStorage.getItem("test");=> null

As i have read from different posts, they can normally work with localStorage in Selenium.

like image 317
Lukas S Avatar asked Feb 17 '18 12:02

Lukas S


1 Answers

Javascript / Node

I had the same problem for the reasons explained by previous answers ; local storage is per profile and Selenium opens with a new profile and a new empty local storage each time.

To keep the local storage across selenium page launches, use the same profile:

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

const chromeProfilePath = 'C:\\Users\\Bob\\AppData\\Local\\Google\\Chrome\\User Data';

let options = new chrome.Options();
options.addArguments(`--user-data-dir=${chromeProfilePath}`); 
let driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

You can get the profile path by typing chrome://version in the browser.

IMPORTANT: remove the "default" from the end of that path as chrome adds it back on again.

Also, changes to Chrome/ChromeDriver post v74 require associated changes in selenium-webdriver for options to work - make sure you have the appropriate version of selenium-webdriver.

like image 159
Bob Avatar answered Sep 28 '22 16:09

Bob