Web Scraping with VBA - problems clicking a button (button is only class)

Pr0x1mo

New Member
Joined
May 14, 2015
Messages
20
So i got everything working to go to the website, enter in the username and password but i don't know how to change this so that it clicks the "sign in" button.


Code:
Private Sub Open_multiple_sub_pages_from_main_page()




Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim valeur_heure As Object




' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True


' Send the form data To URL As POST binary request
IE.navigate "https://illinois.tylerhost.net/OfsWeb/FileAndServeModule"


' Wait while IE loading...
While IE.Busy
        DoEvents
Wend




Set objCollection = IE.document.getElementsByTagName("input")
'Set objCollection = IE.document.getElementsByClassName("btn btn-primary")
i = 0
While i < objCollection.Length
    If objCollection(i).Name = "UserName" Then
        ' Set text for search
        objCollection(i).Value = "me@somewhere.com"
    End If
    If objCollection(i).Name = "Password" Then
        ' Set text for search
        objCollection(i).Value = "mypassword"
    End If


    If objCollection(i).Class = "btn btn-primary" Then ' submit button if found and set
        Set objElement = objCollection(i)
    
    End If
    i = i + 1
Wend
objElement.Click    ' click button to load page


While IE.Busy
        DoEvents
Wend


' Show IE
IE.Visible = True
Set Doc = IE.document


Dim links, link


Dim j As Integer                                                                    'variable to count items
j = 0
Set links = IE.document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
While j <= n                                    'loop to go thru all "a" item so it loads next page
    links(j).Click
    While IE.Busy
        DoEvents
    Wend
    '-------------Do stuff here:  copy field value and paste in excel sheet.  Will post another question for this------------------------
    IE.document.getElementById("DetailToolbar1_lnkBtnSave").Click              'save
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    IE.document.getElementById("DetailToolbar1_lnkBtnCancel").Click            'close
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)                                   'wait
    Loop
    Set links = IE.document.getElementById("dgTime").getElementsByTagName("a")
    j = j + 2
Wend
End Sub

Of course this is not working since the code for the button is as follows:

HTML:
  Sign In



So how i would i write the above so that it clicks the button by Class?
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I asked for your username and password to be removed before replying! Thanks Fluff.

Try this:
Code:
    Dim loginButton As Object
    Set loginButton = IE.document.getElementsByClassName("btn btn-primary")(0)    
    loginButton.Click
    While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
 
Upvote 0
I asked for your username and password to be removed before replying! Thanks Fluff.

Try this:
Code:
    Dim loginButton As Object
    Set loginButton = IE.document.getElementsByClassName("btn btn-primary")(0)    
    loginButton.Click
    While IE.Busy Or IE.readyState <> 4: DoEvents: Wend


This works, thanks!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top