单击网站按钮并刮取结果

时间:2017-02-15 15:37:22

标签: vba excel-vba web-scraping excel

我正在写作,我可以通过宏在网站上执行表单。我能够打开Internet Explorer并正确传递所有变量,但是当提交时,我有点迷失。

Sub ESTRAI()
    Dim objIE As Object
    Dim objElement As Object

    ' Create InternetExplorer Object
    Set objIE = CreateObject("InternetExplorer.Application")

    ' Make sure the Internet Explorer window is visible
    objIE.Visible = True

    ' Navigate to your URL
    objIE.Navigate "https://www.trainline.eu/search/milano/roma/2017-02-16-06:00"

    ' Wait while IE loading...
    Do While objIE.Busy
        Application.Wait DateAdd("s", 3, Now)
    Loop

    ' Get the button
    Set objElement = objIE.Document.getElementsByClassName("search__button ember-view progress-button")
    ' Click the button
    objElement.Click        

End Sub

1 个答案:

答案 0 :(得分:0)

在对象元素的末尾指定“0”。

Sub ESTRAI()
Dim objIE As Object
Dim objElement As Object

' Create InternetExplorer Object
Set objIE = CreateObject("InternetExplorer.Application")

' Make sure the Internet Explorer window is visible
objIE.Visible = True

' Navigate to your URL
objIE.Navigate "https://www.trainline.eu/search/milano/roma/2017-02-16-06:00"

' Wait while IE loading...
Do While objIE.Busy
    Application.Wait DateAdd("s", 3, Now)
Loop

While objIE.Busy
    DoEvents
Wend
Do Until objIE.readyState = 4
    DoEvents
Loop
stop
'Get button and search
objIE.Document.getElementsByClassName("button progress-button--button")(0).Click

End Sub