admin管理员组

文章数量:1296458

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.

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.

Share Improve this question edited Feb 17, 2018 at 12:59 Lukas S asked Feb 17, 2018 at 12:11 Lukas SLukas S 3351 gold badge3 silver badges15 bronze badges 2
  • 2 Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – undetected Selenium Commented Feb 17, 2018 at 12:20
  • Could you expand a little more on what you mean when you say you can't read the local storage from the normal browser. I.e. Do you mean you can't read it when it's adding items to the local storage on the Selenium Browser? I'm not sure of the solution myself, but I'm keen to discover if there is one, so upvote from me! – JO3-W3B-D3V Commented Feb 17, 2018 at 12:20
Add a ment  | 

3 Answers 3

Reset to default 4

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.

Thought localStorage is persistent across browser's re-openings - as opposed to sesionStorage which does not survive even a tab close, that persistence is per browser profile.

Whenever Chrome and Firefox browser sessions get created, by default they start off with a new (almost blank) profile - and now you can see why your localStorage data got wiped.

In your example, when you created the entries in the first session they were in fact stored; but once you started the new session, it was in a different profile - and its localStorage was empty.

This default behavior (of starting with a new profile) does make sense - if the browser's localStorage was shared/survived between browsers re-openings, then a previous session's data would pollute the environment for consecutive tests, leading to unexpected results. Consider the case where a session stores in localStorage a setting "the user wants to hide this information from the UI", and a follow-up suite's purpose is to verify that same information - its environment is changed from the norm ("polluted"), and leads to unexpected state.

The default behavior can be overridden - the browsers can be started with specific profiles, thus giving the persistence if needed.

I said Chrome and Firefox start with a new profile, yet IE is started with a default one, which probably will persist localStorage - but don't quote me on this :)


Finally, to address a misconception - localStorage is saved on the user's disk drive (thus it's persisted), not in the browser's memory; sessionStorage is purely in-memory (another way to guarantee it won't survive browser re-opening).

The question is "where is the data being stored"? The answer is that it is stored in the browser, not in Selenium. Therefore, when the last browser tab or window is closed, the data is lost forever.

If you open a browser and use localStorage.setItem, the data will be stored in the browser memory. And then you open another tab or window from that session and close only the first tab/window, then the browser still retains the data. But as soon as you close the last tab/window, the data will be lost.

The conclusion is that if you want to use data across browser windows, you must do so before closing the last window or tab.

本文标签: javascriptLocalStorage in Selenium WebdriverStack Overflow