Home > database >  Why localStorage.getItem given none?
Why localStorage.getItem given none?

Time:01-03

why return window.localStorage.getItem always given none ? Even though when you see it there is value.

import undetected_chromedriver as webdriver
import time


recreate_localStorage_script = '''
    const iframe = document.createElement('iframe');
    document.head.append(iframe);
    const pd = Object.getOwnPropertyDescriptor(iframe.contentWindow, 'localStorage');
    iframe.remove();    
    Object.defineProperty(window, 'localStorage', pd);
    '''

options = webdriver.ChromeOptions()
profile = "C:\\Users\\nicvaldy\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 38"
options.add_argument(f"user-data=dir={profile}",)
mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile Safari/535.19" }
chrome_options = webdriver.ChromeOptions()

chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(options=options,use_subprocess=True)
driver.get("https://click.discord.com/ls/click?xxxxxxxxxxxxxxxx")
driver.execute_script(recreate_localStorage_script)
print(driver.execute_script(f"return window.localStorage.getItem('token');"))

enter image description here

This is the result

please help me to solve this problem, thanks

CodePudding user response:

You are getting the local storage value right after making the request but there might be a delay before the token is set. Can you try waiting by some ammount after making the request and see if it’s that?

  • Related