首页 > 解决方案 > 接收 KeyNotFoundError 字典键未找到:使用 .click 方法时的状态

问题描述

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

我从以下站点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 并单击“我感到幸运”按钮的简单代码。

标签: excelvbaweb-scrapingautomationselenium-chromedriver

解决方案


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

尝试用 2.46 版替换您的 Chromedriver。这是链接: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

推荐阅读