如何通过在我的 vba 网络浏览器文档中输入 Class 来获取所有元素?
How can I get all Elements by Class type in my vba webbrowser document?
下面的代码是 Excel 2010 表单,上面有一个网络浏览器控件。
在 VBE 参考部分选择了 Microsoft HTML 对象库,我使用的是 VBA 7.0。
Option Explicit
Private Sub UserForm_Initialize()
WebBrowser1.Navigate "http://www.google.com/"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Inputs As IHTMLElementCollection
'returns inputs on page where class name is "gsfi"
Set Inputs = wb.Document.GetElementsByClassName("gsfi")
'returns all inputs on a page
Set Inputs = WebBrowser1.Document.GetElementsByTagName("Input")
Debug.Print Inputs.Length
End Sub
编辑: 我正在根据@Tim Williams 的帮助修改这个问题,以说明我真正想要的是什么。我怎样才能得到标签名称 "Input" 和 Class 类型为 "Text" 或 "Button" 等的所有元素?
您确定有 class 名称的元素吗?
下面的代码表示 class...
没有输入
编辑:"Type" 是您要查找的内容:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Inputs As IHTMLElementCollection, o As Object
Set Inputs = pDisp.Document.getElementsByTagName("input")
Debug.Print Inputs.Length '>>11
For Each o In Inputs
Debug.Print o.Name, o.tagName, o.Type
Next o
End Sub
下面的代码是 Excel 2010 表单,上面有一个网络浏览器控件。
在 VBE 参考部分选择了 Microsoft HTML 对象库,我使用的是 VBA 7.0。
Option Explicit
Private Sub UserForm_Initialize()
WebBrowser1.Navigate "http://www.google.com/"
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Inputs As IHTMLElementCollection
'returns inputs on page where class name is "gsfi"
Set Inputs = wb.Document.GetElementsByClassName("gsfi")
'returns all inputs on a page
Set Inputs = WebBrowser1.Document.GetElementsByTagName("Input")
Debug.Print Inputs.Length
End Sub
编辑: 我正在根据@Tim Williams 的帮助修改这个问题,以说明我真正想要的是什么。我怎样才能得到标签名称 "Input" 和 Class 类型为 "Text" 或 "Button" 等的所有元素?
您确定有 class 名称的元素吗? 下面的代码表示 class...
没有输入编辑:"Type" 是您要查找的内容:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim Inputs As IHTMLElementCollection, o As Object
Set Inputs = pDisp.Document.getElementsByTagName("input")
Debug.Print Inputs.Length '>>11
For Each o In Inputs
Debug.Print o.Name, o.tagName, o.Type
Next o
End Sub