我需要使用VBA找到并按下网页上的按钮

时间:2013-06-18 22:09:21

标签: vba dom mshtml

我写了一个宏,它将打开一个网页,找到我需要放入数据的位置,然后我想让宏点击预填充按钮,然后点击确定。

按钮的页面源代码是:

<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">

我整个星期都在寻找答案,我觉得我已经基本了解了这应该如何通过运行循环来搜索它,但我没有运气实际得到这个上班。

有人可以告诉我将在我的页面中搜索此按钮的循环吗?

提前谢谢你。

到目前为止请求的代码

Private Sub Populate_Click()
    Dim i As Long
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True
    IE.Navigate "website" 'make sure you've logged into the page

    Do
        DoEvents
    Loop Until IE.READYSTATE = 3

    Do
        DoEvents
    Loop Until IE.READYSTATE = 4
    ActiveSheet.EnableCalculation = False
    ActiveSheet.EnableCalculation = True
    Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
    Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)

    Set objCollection = IE.document.getElementsByTagName("input")
    i = 0
    While i < objCollection.Length
        If objCollection(i).Type = "button" And _
            objCollection(i).Name = "Prefill" Then
                Set objElement = objCollection(i)
        End If
        i = i + 1
    Wend
    objElement.Click
End Sub

1 个答案:

答案 0 :(得分:4)

看起来你非常接近,这就是我习惯做类似的事情

    With ie.document

        Set elems = .getelementsbytagname("input")
        For Each e In elems
            If (e.getattribute("className") = "saveComment") Then
                e.Click
                Exit For
            End If
        Next e

    End With

您可能只需要更改if语句。

我还注意到您的代码引用了.Name = "Prefill",但您的html代码段引用了.value = "Prefill"