2

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
andrew
  • 41
  • 7
  • I just answered a very similar question on this question. http://stackoverflow.com/questions/43095155/vba-to-complete-a-internet-form/43096864?noredirect=1#comment73287084_43096864 – Miguel Mar 29 '17 at 19:54

2 Answers2

1

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

Miguel
  • 2,019
  • 4
  • 29
  • 53
  • thank you, but i'm getting an error 424. I tried setting dados as an object but it didn't help. I do have my references set correctly – andrew Mar 29 '17 at 20:53
  • @andrew, my fault copy paste got me again. Try one more time. I Fixed the on the for each loop I had the wrong `Var` – Miguel Mar 29 '17 at 21:28
  • Thank you! it works great. I changed Debug.print mybtn(i).innerText to Sheets("sheet1").Range("A" & i + 1) = mybtn(i).innerText. otherwise nothing printed. But i wish to have the webpage visible as well. I tried setting HTMLDocument.visible = true in the with loop but it didn't work. I'm use to using IE for these type of applications. Should i rewrite the code fro IE or is there a short cut im missing? Thank you again, you have been very helpful! – andrew Mar 30 '17 at 12:49
  • @Andrew, glad to help. Don't forget to set it as the answer! – Miguel Mar 30 '17 at 12:52
0

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
ASH
  • 20,759
  • 19
  • 87
  • 200