提取网页vba上所有输入框的列表

提取网页vba上所有输入框的列表,vba,Vba,我想在Excel上创建一个网页上所有输入框标签的列表,因此我设想代码如下: Sub IEInteract() Dim i As Long Dim URL As String Dim IE As Object Dim objCollection As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True URL = "mywebsite.com" IE.Navigate URL Do

我想在Excel上创建一个网页上所有输入框标签的列表,因此我设想代码如下:

Sub IEInteract()

Dim i As Long
Dim URL As String
Dim IE As Object
Dim objCollection As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True

URL = "mywebsite.com"

IE.Navigate URL

Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop

objCollection = IE.Document.getElementsByTagName("input")

For Each el In objCollection
label = el.label 'or something like that????'
Debug.Print label

Next el

End Sub
我哪里做错了?谢谢

顺便说一句,我的VBA还可以,但我的HTML不存在

  • 出于学习目的,可以选择一个具有更明显输入框的网站,而不是下拉列表
  • 许多输入框不会被预先填充,所以可能会考虑读取检索到的元素的其他属性。或者甚至向它们写入数据,然后检索这些值
  • 按标记名选择可以带回许多您可能没有预料到的项目
  • 牢记以上所有内容。尝试运行以下命令,它将生成
    标记元素的集合

    代码:

    Option Explicit
    
    Public Sub PrintTagInfo()
        'Tools > references > Microsoft XML and HTML Object library
        Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
        Dim html As New HTMLDocument
    
        With http
            .Open "GET", "https://www.mrexcel.com/forum/register.php", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Dim inputBoxes As MSHTML.IHTMLElementCollection, iBox As MSHTML.IHTMLElement, i As Long
    
        Set inputBoxes = html.getElementsByTagName("input") '<== the collection of input tags on the page
        '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
        For Each iBox In inputBoxes
            Debug.Print "Result #" & i + 1
            Debug.Print vbNewLine
            Debug.Print "ID: " & iBox.ID '<== select a sample of properties to print out as some maybe empty
            Debug.Print "ClassName: " & iBox.className,
            Debug.Print "Title: " & iBox.Title
            Debug.Print String$(20, Chr$(61))
            Debug.Print vbNewLine
            i = i + 1
        Next iBox
    End Sub
    
    Option Explicit
    Public Sub PrintTagInfo()
        'Tools > references > Microsoft XML and HTML Object library
        Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
        Dim html As New HTMLDocument
    
        With http
            .Open "GET", "https://www.mrexcel.com/forum/register.php", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Dim inputBoxes As Object, i As Long
    
        Set inputBoxes = html.querySelectorAll("input[type=""text""]") '<== the collection of text input boxes on page. Returned as a NodeList
        '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
        For i = 0 To inputBoxes.Length - 1
            Debug.Print "Result #" & i + 1
            Debug.Print vbNewLine
            Debug.Print "ID: " & inputBoxes.Item(i).ID '<== select a sample of properties to print out as some maybe empty
            Debug.Print "ClassName: " & inputBoxes.Item(i).className,
            Debug.Print "Title: " & inputBoxes.Item(i).Title
            Debug.Print String$(20, Chr$(61))
            Debug.Print vbNewLine
        Next i
    End Sub
    
    样本结果:

    Option Explicit
    
    Public Sub PrintTagInfo()
        'Tools > references > Microsoft XML and HTML Object library
        Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
        Dim html As New HTMLDocument
    
        With http
            .Open "GET", "https://www.mrexcel.com/forum/register.php", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Dim inputBoxes As MSHTML.IHTMLElementCollection, iBox As MSHTML.IHTMLElement, i As Long
    
        Set inputBoxes = html.getElementsByTagName("input") '<== the collection of input tags on the page
        '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
        For Each iBox In inputBoxes
            Debug.Print "Result #" & i + 1
            Debug.Print vbNewLine
            Debug.Print "ID: " & iBox.ID '<== select a sample of properties to print out as some maybe empty
            Debug.Print "ClassName: " & iBox.className,
            Debug.Print "Title: " & iBox.Title
            Debug.Print String$(20, Chr$(61))
            Debug.Print vbNewLine
            i = i + 1
        Next iBox
    End Sub
    
    Option Explicit
    Public Sub PrintTagInfo()
        'Tools > references > Microsoft XML and HTML Object library
        Dim http As New XMLHTTP60                    '<== this will be specific to your excel version
        Dim html As New HTMLDocument
    
        With http
            .Open "GET", "https://www.mrexcel.com/forum/register.php", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Dim inputBoxes As Object, i As Long
    
        Set inputBoxes = html.querySelectorAll("input[type=""text""]") '<== the collection of text input boxes on page. Returned as a NodeList
        '<== These are input boxes i.e. you are putting info into them so perhaps populate and then try to read what is in the entry box?
        For i = 0 To inputBoxes.Length - 1
            Debug.Print "Result #" & i + 1
            Debug.Print vbNewLine
            Debug.Print "ID: " & inputBoxes.Item(i).ID '<== select a sample of properties to print out as some maybe empty
            Debug.Print "ClassName: " & inputBoxes.Item(i).className,
            Debug.Print "Title: " & inputBoxes.Item(i).Title
            Debug.Print String$(20, Chr$(61))
            Debug.Print vbNewLine
        Next i
    End Sub
    
    注意:我已经编辑了间距,以便更适合图像

    通过VBE>工具>引用添加的引用


    最后两个是感兴趣的。最下面的一个是版本特定的,您需要重新编写
    XMLHTTP60
    ,如果不使用Excel 2016,它将用于
    xml6.0
    ,以您的Excel版本为目标。

    您这么说,我想代码应该是这样的:不,您不能这样想象。选择一个与您正在使用的网页大致相同的网页,然后描述如何操作。这太他妈的宽泛了。@SIM谢谢你让我知道。我正在练习概括,但这里有一个例子。区别在哪里?非常有用!那么“input”是“tagname”吗?另外,您的XML和my:set IE=CreateObject(“InternetExplorer.Application”)等之间有什么区别吗?XML要快得多,而且您正在打开浏览器。输入可能意味着不同的事情,但如果你是目标,那么是的,它是一种标签类型。再次感谢你。我已经能够复制它来打印来自所有标签的内部文本。是否有“label.for”属性?我无法让它工作。你可以到这里查找主要信息。除此之外,右键单击网页并检查。或者在chrome中使用类似F12的开发工具。那里有丰富的信息。允许您检查xpath、css、标记………刚刚找到它-它本身是(htmlFor)而不是“for”。这是一个VBA问题,而不是HTML问题。非常感谢你的帮助。