You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何window.localStorage.getItem始终返回None?明明存在对应值

问题:调用window.localStorage.getItem始终返回None,但实际值存在

明明在页面中能确认token值存在,但通过undetected-chromedriver执行代码后,window.localStorage.getItem('token')始终返回None,相关代码如下:

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');"))

解决办法

1. 修复配置项冲突与参数错误

你创建了两个独立的ChromeOptions实例,但初始化driver时只传入了options,导致移动模拟配置未生效;同时用户目录参数名写错(应为user-data-dir而非user-data=dir),导致未加载指定的用户配置文件。

修改配置代码如下:

import undetected_chromedriver as webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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"
}
# 将移动模拟配置合并到同一个options实例
options.add_experimental_option("mobileEmulation", mobile_emulation)

2. 等待页面跳转完成后再操作

你访问的是Discord跳转链接,页面会自动跳转到目标页面,跳转前后的localStorage上下文不同,提前执行脚本会读取错误的上下文。添加等待逻辑,确保页面跳转完成后再操作:

driver = webdriver.Chrome(options=options, use_subprocess=True)
driver.get("https://click.discord.com/ls/click?xxxxxxxxxxxxxxxx")
# 等待URL包含discord.com(根据实际跳转后的URL调整等待条件)
WebDriverWait(driver, 10).until(EC.url_contains("discord.com"))
# 执行重写脚本并读取token
driver.execute_script(recreate_localStorage_script)
print(driver.execute_script(f"return window.localStorage.getItem('token');"))

3. 验证localStorage重写是否生效

可以先打印localStorage实例,确认重写脚本是否成功修改了window.localStorage:

print(driver.execute_script("return window.localStorage;"))

如果输出为null或异常实例,说明重写脚本未生效,可尝试调整脚本注入时机(比如使用driver.execute_cdp_cmd在页面加载前注入脚本)。


内容的提问来源于stack exchange,提问作者nicvaldy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 06:55:55