Excel VBA无法获取元素并在网站上输入数据

时间:2018-07-19 03:32:38

标签: excel vba excel-vba web-scraping

我正在尝试编写VBA脚本,以将关键字(例如:Amuse)输入以下网站的文本框“地图定位”,然后单击“开始”以自动搜索该地点。 CentaMap

这是我为文本框找到的html脚本

<INPUT onkeyup=searchBoxTextChanged(this.value); id=qbyid style="FONT-SIZE: 10pt" maxLength=60 name=q autocomplete="off">

知道正常的做法是通过id使用get元素,然后在其中输入这样的数据。但是我被困住了,因为我无法使用以下代码获取textbox元素:

Sub SubCentalineAutomation()
    Dim myIE As InternetExplorer
    Const url As String = "http://hk.centamap.com/gc/home.aspx?lg=en"

    Set myIE = New InternetExplorer

    myIE.navigate (url)

    Do While myIE.readyState <> 4
        DoEvents
    Loop
    myIE.Visible = True

    myIE.document.getElementsByName("q")(0).Value = "Amuse" 
End Sub

我尝试使用getElementsById(“ qbyid”)代替代码,但是VBA也找不到该元素。 有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

尝试getElementById("qbyid"),而不是getElementsById("qbyid")

答案 1 :(得分:0)

需要参考

  • Microsoft Internet控件
  • Microsoft HTML对象库

Sub SubCentalineAutomation()
    Dim myIE As InternetExplorer
    Dim frame As MSHTML.HTMLFrameElement
    Dim inp As MSHTML.HTMLInputElement

    Const url As String = "http://hk.centamap.com/gc/home.aspx?lg=en"

    Set myIE = New InternetExplorer

    myIE.navigate (url)

    Do While myIE.readyState <> 4
        DoEvents
    Loop
    myIE.Visible = True

    Set frame = myIE.document.getElementsByName("search")(0)
    Set inp = frame.contentDocument.getElementsByName("q")(0)
    inp.Value = "Amaze"
End Sub

答案 2 :(得分:0)

如果您只是想要建议,而没有地图导航,则可以使用:

Option Explicit
Public Sub GetInfo()
    Dim sResponse As String, html As New HTMLDocument, searchTerm As String
    searchTerm = "Amuse"
    With CreateObject("MSXML2.XMLHTTP")
            .Open "GET", "http://hk.centamap.com/gsearch/paddresssearch1.aspx?lg=en&search=" & searchTerm & "&ck=gbase&ft2=", False
            .send
            sResponse = StrConv(.responseBody, vbUnicode)
            sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))

            With html
                .body.innerHTML = sResponse
                 Debug.Print html.querySelector("a").innerText
            End With
    End With
End Sub

如果要输入一个值,然后导航到第一个返回的建议:

Option Explicit
Public Sub SubCentalineAutomation()
    Dim myIE As InternetExplorer, html As New MSHTML.HTMLDocument, frame As MSHTML.HTMLFrameElement, form As MSHTML.HTMLFormElement
    Const URL As String = "http://hk.centamap.com/gc/home.aspx?lg=en"
    Set myIE = New InternetExplorer

    With myIE
        .navigate URL
        .Visible = True
        While .Busy Or .readyState < 4: DoEvents: Wend

        Set html = .document
        Set frame = html.getElementsByTagName("frame")(1)
        Set form = frame.contentDocument.getElementsByTagName("form")(0)
        form.getElementsByTagName("input")(1).Value = "Amuse"
        form.getElementsByTagName("input")(2).Click

        While .Busy Or .readyState < 4: DoEvents: Wend

        Set frame = .document.getElementsByTagName("frame")(4)
        Set frame = frame.contentDocument.getElementsByTagName("iframe")(0) 
        frame.contentDocument.getElementsByTagName("table")(0).getElementsByTagName("a")(0).Click

        '.Quit '<== Remember to quit application
    End With

End Sub