想要使用excel vba选择网站上的按钮

时间:2017-03-29 19:16:19

标签: excel vba web-scraping

我想使用excel来浏览网页。但该网站不像普通网站那样使用ID(亚马逊,谷歌,等)。该网站是http://www.scoopmae.com/。我如何选择“预订演示”按钮。我通常会使用getelementbyID,但我不知道id。我也试过标签和课,但没有运气。

Sub scoop()
Set objie = CreateObject("InternetExplorer.Application")
objie.Top = 0
objie.Left = 0
objie.Width = 1600
objie.Height = 900
objie.Visible = True 'We can see IE

On Error Resume Next
objie.navigate ("http://scoopmae.com")
Do
DoEvents
Loop Until objue.readystate = 4

Application.Wait (Now + TimeValue("0:00:02"))
'MsgBox ("wait")
'click button
'objie.document.getElementsById("scoop-sort").Click



 x = objie.document.getElementsByClassName("a")
Cells(1, 2) = x
End Sub

2 个答案:

答案 0 :(得分:1)

我可以通过这样做来访问该按钮。

Sub test()


Dim oHtml As HTMLDocument
Dim oElement As Object

Set oHtml = New HTMLDocument

With CreateObject("WINHTTP.WinHTTPRequest.5.1")
    .Open "GET", "http://www.scoopmae.com/", False
    .send
    oHtml.body.innerHTML = .responseText
End With

Set mybtn = oHtml.getElementsByClassName("sf-button large orange default  dropshadow")(0).getElementsByTagName("span")

i = 0
For Each oElement In mybtn
    Debug.Print mybtn(i).innerText
    i = i + 1
Next oElement

End Sub

确保您转到工具 - >引用并添加对Microsoft HTML对象库的引用[MSHTML.TLB] 谢谢 米格尔

答案 1 :(得分:0)

你基本上就在那里!确保添加2个引用: 1 Microsoft Internet Controls 2. Microsoft HTML对象库 阅读更多http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-v

请参阅此链接。

http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

以下是代码:

Sub test()


Dim oHtml As HTMLDocument
Dim oElement As Object

Set oHtml = New HTMLDocument

With CreateObject("WINHTTP.WinHTTPRequest.5.1")
    .Open "GET", "http://www.scoopmae.com/", False
    .send
    oHtml.body.innerHTML = .responseText
End With

Set mybtn = oHtml.getElementsByClassName("sf-button large orange default  dropshadow")(0).getElementsByTagName("span")

i = 0
For Each oElement In mybtn
    Debug.Print mybtn(i).innerText
    oElement.Click
    i = i + 1
Next oElement

End Sub