要使用Excel VBAselect网站上的button

我想用Excel来浏览一个网页。 但网站不使用像一个正常的网站ID(亚马逊,谷歌,等)。 该网站是http://www.scoopmae.com/ 。 我将如何select“预订演示”button。 我通常会使用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 

我可以通过这样访问button。

 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]谢谢米格尔

你基本上就在那里! 请确保您添加2个引用:1 Microsoft Internet控件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