andrew
andrew

Reputation: 41

Want to select button on website using excel vba

I want to use excel to navigate a webpage. yet the website doesn't use ID's like a normal site does (amazon,google,ect). the website is http://www.scoopmae.com/. how would i select the "book a demo" button. I would normally use getelementbyID, but i don't know the id. I've also tried tag and class but no luck.

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

Upvotes: 2

Views: 920

Answers (2)

ASH
ASH

Reputation: 20302

You are basically right there!! Make sure you add 2 references: 1 Microsoft Internet Controls 2. Microsoft HTML Object Library Read more at http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-v

See this link.

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

Here is the code:

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

Upvotes: 0

Miguel
Miguel

Reputation: 2079

I can access the button by doing this.

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

Make certain that you go to Tools -> References and add a reference to the Microsoft HTML Object Library [MSHTML.TLB] Thanks Miguel

Upvotes: 1

Related Questions