首页 > 解决方案 > 通过 Excel VBA 单击 Internet Explorer 上的按钮

问题描述

我正在尝试通过 Excel 单击按钮。

重点是工作。但点击没有。

Set objCollection = IE.document.getElementsByTagName("button")
While i < objCollection.Length
    If objCollection(i).Type = "submit" And objCollection(i).innerText = "Add items" Then
        Set objElement = objCollection(i)
        objElement.Focus
        objElement.Click
    End If
    i = i + 1
Wend

按钮代码

标签: excelvba

解决方案


我做了一个快速测试:

' Add references to the project:
' « Microsoft Internet Controls » and « Microsoft HTML Object Library »
Sub Test()
    Dim ie, button As Object
    Dim ht As HTMLDocument
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate ("https://www.sampleforms.com/")
    Do Until ie.readyState = 4
       DoEvents
    Loop
        
    Set ht = ie.document
    ie.document.querySelector("#searchVal").Value = "yaya"
    Set button = ie.document.querySelector(".search-btn")
    button.Click
    Do Until ie.readyState = 4
       DoEvents
    Loop
End Sub

所以尝试:

Set button = ie.document.querySelector("#catalog.items.add")

推荐阅读