【问题标题】:Receiving KeyNotFoundError Dictionary key not found: status when using the .click method接收 KeyNotFoundError 字典键未找到:使用 .click 方法时的状态
【发布时间】:2019-10-23 03:06:45
【问题描述】:

我目前正在尝试在 Excel 中学习 VBA,并且我编写了一个简单的程序,我应该能够启动 Chrome 并单击一个按钮。问题是每当我尝试使用.click 方法时,我都会收到一个运行时错误“0”:KeyNotFoundError Dictionary key not found:status

我从以下站点 https://florentbr.github.io/SeleniumBasic/ 下载了 Selenium Basic,在 VBA 中激活了引用并下载了最新的 chromedriver

Sub driver()

    Dim driver As New Selenium.WebDriver
    Set driver = New Selenium.WebDriver

    driver.Start "chrome"
    driver.Get "http://www.google.com"
    Set Element = driver.FindElementByName("btnI")
    Element.Click

End Sub

这只是我启动 Chrome 并转到 Google 并单击“我感到幸运”按钮的简单代码。

【问题讨论】:

  • 不是问题,但如果您在下一行执行Set ... = New ...,请不要Dim ... As New。事实上,尽可能避免使用Dim ... As New:它会生成一个自动实例化的对象,并且会带来您在这种情况下不需要的副作用。

标签: excel vba web-scraping automation selenium-chromedriver


【解决方案1】:

触发该按钮的快速测试,我需要使用 javascript

driver.ExecuteScript "document.querySelector('[name=btnI]').click();"

如果错误仍然存​​在,我建议检查 chromedriver.exe is the right version for your Chrome browser 并重新安装 selenium 并像往常一样在 VBE 中检查 selenium 类型引用。

【讨论】:

  • 这条线有效。我想我会从现在开始使用它,直到找到替代方案。我不确定为什么前面的代码会给我这样的错误。
【解决方案2】:

最新版 Chrome 抛出“KeyNotFoundError Dictionary key not found:status”错误。

尝试将您的 Chromedriver 替换为 2.46 版。这是链接:https://chromedriver.storage.googleapis.com/index.html?path=2.46/

以下是我尝试过的代码,它对我来说非常好用。让我们知道此解决方案是否适合您! :)

   Sub driver()

        Dim driver As New Selenium.WebDriver
        Set driver = New Selenium.WebDriver

        driver.Start "chrome"
        driver.Get "http://www.google.com"
        driver.FindElementByCss("#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input[type=submit]:nth-child(2)").Click

    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2020-09-19
    • 2015-12-10
    • 2023-03-24
    • 2013-07-22
    • 2017-09-08
    相关资源
    最近更新 更多