excel - VBA 无法获取元素和输入数据
问题描述
这是我希望输入数据的网站:网站在“来自城市/机场”下
我为该输入框找到了以下 HTML 元素:
<input name="frm1" class="frm1 col-sm-12 ui-autocomplete-input" autocomplete="off">
下面是我的 VBA 代码:
Sub Data_Scrap()
Dim IE As SHDocVw.InternetExplorer, HTMLDoc As MSHTML.HTMLDocument, HTMLInput As MSHTML.IHTMLElementCollection, x As MSHTML.IHTMLElement
Set IE = New SHDocVw.InternetExplorer
IE.Visible = True
IE.navigate "https://www.icao.int/environmental-protection/Carbonoffset/Pages/default.aspx"
'Wait until the website is loaded
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementsByTagName("input")
For Each x In HTMLInput
If x.getAttribute("name") = "frm1" Then
x.innerText = "hkg"
Exit For
End If
Next
End Sub
我也尝试了一些更简单的方法,HTMLDoc.getElementsByTagName("frm1")(0).value = "hkg"
但它出现了错误:
未设置对象变量或 With 块变量
解决方案
推荐阅读
- html - Xpath - 复杂路径:如何通过引用不同父级中的另一个元素来查找元素?
- haskell - 如何调整上下文约束以访问列表中的 monad 转换器功能?
- python-3.x - 需要整数的输入的 Python 错误处理
- python - 除非我打开整个文件夹,否则 PyQt 应用程序将无法正常运行
- c# - C# 中的 Image 类在哪里?
- javascript - 如何将侦听器附加到 Django 管理员中的日期选择?
- java - Thorntail 从 Java 属性文件加载外部属性
- statsmodels - 动态因子模型:预测因子
- amazon-web-services - 当我们每毫秒的传入过多时,如何将来自 Lambda 函数的数据保存到 S3 中?
- mongodb - 在这种要求中使用 $mul 的正确原型是什么?