Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there a method to set a local storage value? #2722

Answered by mdmintz
STARKILLER-1 asked this question in Q&A
Discussion options

Is there a method to set a local storage value? I am doing it by driver.execute_script(f"window.localStorage.setItem('wbx__tokenData', '{json.dumps({'token': token})}')") but when I change page local storage clears my token

You must be logged in to vote

For the SB() or BaseCase formats, you can use set_local_storage_item(key, value) to set a local_storage value.

self.set_local_storage_item(key, value)

Replies: 1 comment 4 replies

Comment options

For the SB() or BaseCase formats, you can use set_local_storage_item(key, value) to set a local_storage value.

self.set_local_storage_item(key, value)
You must be logged in to vote
4 replies
Comment options

And for the Driver() object?

Comment options

The Driver() is limited compared to SB(). Either use SB() if you need the extra methods/functionality, or create your own methods that do what you need.

Comment options

Hello, Michael. Why does local storage variable, which i've set via set_local_storage_item, after refreshing page is deleted?

 with SB(
 uc=True,
 browser="chrome",
 locale="ru",
 position="0,0",
 agent=browser_config.get("user_agent"),
 window_size=browser_config.get("window_size"),
 ad_block=True,
 devtools=True
 ) as driver:
 def login(driver: SeleniumBase, token: str):
 logger.info("Execute token to LocalStorage")
 temp = json.dumps({"token": token})
 driver.set_local_storage_item("wbx__tokenData", temp)

While we were working with raw selenium we did it like this:

 driver.execute_script("localStorage.setItem(arguments[0], JSON.stringify({token: arguments[1]}));",
 'wbx__tokenData', token)

And it were working fine.

Comment options

As of 4.35.7 you have these CDP Mode methods:

sb.cdp.set_local_storage_item(key, value)
sb.cdp.get_local_storage_item(key)

And if you're calling things from regular mode, use:

sb.set_local_storage_item(key, value)
sb.get_local_storage_item(key)

Any method called from driver is using the raw Selenium API... (not the SeleniumBase one).
If there's an issue with a raw Selenium method, then you would have to check with their repo.

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /