【问题标题】:Accessing a website's Local Storage Key/Value Items using VBA and Selenium returns Empty values使用 VBA 和 Selenium 访问网站的本地存储键/值项返回空值
【发布时间】:2021-09-07 16:09:57
【问题描述】:

我手动打开 Chrome 浏览器并转到“https://www.google.com/”并按 F12 以转到开发者工具。然后我转到控制台并输入localStorage.key(0) 以获取本地存储中第一项的密钥,然后我得到'whatever'。

但是,当我运行以下测试代码时,firstKey 中的返回值为 Empty。

Sub GetLocalStorage()

    Dim driverGoogle As Object
    Dim firstKey As Variant
    
    Set driverGoogle = CreateObject("Selenium.Chromedriver")
    
    driverGoogle.Get "https://www.google.com/"
    driverGoogle.Window.SetSize 1800, 1050
    
    firstKey = driverGoogle.ExecuteScript("localStorage.key(0);")

End Sub

我什至将localStorage.key(0) 更改为不正确的localStorage.aaakey(0),确保ExecuteScript 确实在执行,并且确实 Selenium 引发了错误。

那么,如何获取本地存储键/值?

【问题讨论】:

    标签: vba selenium selenium-chromedriver


    【解决方案1】:

    您必须添加延迟才能让网站加载。
    目前,您正尝试在页面刚开始加载时将 URL 发送到 Web 驱动程序后立即应用 driverGoogle.ExecuteScript("localStorage.key(0);") 脚本。

    【讨论】:

    • 当我单步执行代码时,该站点已完全加载。所以我可以假设你已经运行了代码并且 firstKey 不为空?
    • 好吧,如果您有时间,请您单步执行此代码,看看您会得到什么。我可能有本地问题。
    • 抱歉,我的机器上根本没有 VBA...
    【解决方案2】:

    ExecuteScript 字符串需要“return”指令。添加后一切正常。

    firstKey = driverGoogle.ExecuteScript("return localStorage.key(0);")

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2017-10-21
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多