-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Is there a method to set a local storage value? #2722
-
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
Beta Was this translation helpful? Give feedback.
All reactions
For the SB()
or BaseCase
formats, you can use set_local_storage_item(key, value)
to set a local_storage
value.
SeleniumBase/help_docs/method_summary.md
Line 602 in b77a1c0
Replies: 1 comment 4 replies
-
For the SB()
or BaseCase
formats, you can use set_local_storage_item(key, value)
to set a local_storage
value.
SeleniumBase/help_docs/method_summary.md
Line 602 in b77a1c0
Beta Was this translation helpful? Give feedback.
All reactions
-
And for the Driver() object?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.