首页 > 解决方案 > 找不到 div 类时出现“内存不足”错误

问题描述

我正在将 Selenium 与 ChromeDriver 一起使用

Dim driver As New ChromeDriver

driver.Get "https://somewebsite.com"

val = driver.FindElementByCss(".some-class-that-does-not-exist-on-this-page").Text

Cells(x, 2).Value = val

当我的 vba 代码在页面上找不到该类时,它会抛出一个

“内存不足”错误 7

即使,我的任务经理说我的记忆力只有 29%。

在此处输入图像描述

当我单击帮助时,它会将我带到https://docs.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/out-of-memory-error-7

我已经尝试过那里建议的事情:

但问题仍然存在。只有在找不到 div 类时才会发生这种情况。

我试图添加一个 if 语句来检查元素是否存在

  If IsObject(driver.FindElementByCss(".some-class-that-does-not-exist-on-this-page")) Then

    val = driver.FindElementByCss(".some-class-that-does-not-exist-on-this-page").Text

    Cells(x, 2).Value = val

  End If

但仍然导致内存不足错误。

标签: excelvbaselenium-chromedriver

解决方案


我通过使用错误处理程序解决了它

On Error GoTo ErrorHandler
    elem = driver.FindElementByCss(".some-class-that-does-not-exist-on-this-page")
    Cells(x, 2).Value = elem.Text
ErrorHandler:
    Cells(x, 2).Value = "HTML element not found"
    Resume Next

推荐阅读