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
Try this code, worked for me to click on
Log In
Code
Edit
You can use this code to get the href:
Or to get the InnerText:
Then you can use conditional such as:
OR