2

I am looking for a VBA code that can help me to click a button from a web page. I got the below VBA from one of the sites which works as expected.

Sub followWebsiteLink()

Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim Link As Object
Dim ElementCol As Object

Application.ScreenUpdating = False

Set ie = New InternetExplorer
ie.Visible = True

ie.navigate "https://www.google.co.in"

Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website…"
DoEvents
Loop

Set html = ie.document
Set ElementCol = html.getElementsByTagName("a")

For Each Link In ElementCol
If Link.innerHTML = "Google Pixel 2" _ 'Or u can use "Advertising"
Then
Link.Click
End If
Next Link

Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

But if I replace it with the site name I want, it wont work.

**The changes I made are as follows **

ie.navigate "https://www.flipkart.com/"


For Each Link In ElementCol
If Link.innerHTML = "Log In" _
And Link.getElementsByClassName = "_2k0gmP" _
Then
Link.Click
End If
Next Link
ChrisM
  • 1,576
  • 6
  • 18
  • 29
dhanya
  • 255
  • 1
  • 12

1 Answers1

1

Try this code, worked for me to click on Log In

Code

#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub test()
Dim IE As Object
Dim htmlDoc As Object
Dim sURL As String

' CREATING OBJECT
Set IE = CreateObject("internetexplorer.application")

sURL = "https://www.flipkart.com/"
' WEBPAGE NAVIGATION
With IE
.navigate (sURL)
.Visible = True
End With

WaitIE IE, 2000

' CLICK ON LOG IN
Set htmlDoc = IE.document
Set buttonclick = htmlDoc.getElementsByClassName("_2k0gmP")

buttonclick(7).Click
WaitIE IE, 5000

'IE.Quit
'Set IE = Nothing
End Sub

Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.readyState = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.readyState = 4 Or Not IE.Busy
End Sub

Edit

You can use this code to get the href:

For Each Element In buttonclick
Debug.Print Element.href
Next

Or to get the InnerText:

For Each Element In buttonclick
Debug.Print Element.innerText & " " & i
i = i + 1
Next
'Where i is the number of the item

Then you can use conditional such as:

For Each Element In buttonclick
If Element.href = "href_link" Then Element.Click
Next

OR

For Each Element In buttonclick
If Element.innerText = "Log In" Then Element.Click
Next
danieltakeshi
  • 887
  • 9
  • 37
  • Thanks @ danieltakeshi, Please guide me how to mention the HREF as well to this code so that I wont land up in clicking some wrong link/button... Infact now this is taking me to a seprate page because ClassName ("_2k0gmP") was not a unique value, there are other tags having same value.... – dhanya Nov 16 '17 at 16:17
  • Yeah, buttonclick(0) is Sell on Flipkart, buttonclick(1) is Advertise, buttonclick(2) is Gift Card, [...], buttonclick(7) is Log In. – danieltakeshi Nov 16 '17 at 16:19
  • @ danieltakeshi `buttonclick(7).Click` works, I really looking forward to see how can we accomodate HREF "/account/login?ret=/" or HTML value "Sign In" to this.... as a safer side.... in future any change in the web site may land up me in wrong button click :) I hope you understand me ! and this will be my last question.... And sorry if I am asking too much question.... If this is not possible please let me know :( – dhanya Nov 16 '17 at 16:36
  • 1
    Thank you @ danieltakeshi – dhanya Nov 16 '17 at 16:44