Excel VBA code to click web button - html

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance

First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub

The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library

Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub

Related

Open a webpage with VBA and click on the "active directory" button to log in

I want to open a web page directly from Excel VBA where I need to click on the "active directory" button which has no ID. This button uses the Windows credentials to automatically log so I just need to click there to later once I'm logged I can start populating some fields.
This generic code is giving me an error message
" The object invoked has disconnected from its clients"
I assume it's related to the web page secure log on.
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub
IE is EOL and shouldn't be used anymore.
I can't tell you how exactly on the page the button is clicked, because that requires the HTML code. But the disconnection from the client may be due to the way IE is initialized. Try it via the GUID D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub

Activating a search button via .Click

I'm using IE11 with the HMTL Object Library and Internet Controls references activated.
There's no element ID on the button but am able to use ie.Document.getElementsByClassName by adding some html and xml declarations thanks to this post.
I'm taking a name and city, state from Excel and plugging it into the website then clicking the search button.
This is where my error occurs.
Run-time error '438': Object doesn't support this property or method.
HTML:
VBA:
Option Explicit
Sub HGScrape()
'Application.ScreenUpdating = False
'we define the essential variables
Dim ie As Object
Dim my_url As String
Dim SearchButton, NameBox, AddressBox
Dim ele As Object
Dim x As Integer
Dim y As Integer
Dim IsOdd As Integer
Dim html_doc As Object 'HTMLDocument
Dim xml_obj As Object 'MSXML2.DOMDocument
my_url = "https://www.healthgrades.com/"
'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
Set ie = New InternetExplorer
ie.Visible = True 'False ''''''''''''''''''''''
ie.Navigate my_url '"13.33.74.92" '("https://www.healthgrades.com/")
While ie.ReadyState <> 4
DoEvents
Wend
Set NameBox = ie.Document.getElementById("search-term-selector-child")
NameBox.Value = ActiveSheet.Range("A2")
Set AddressBox = ie.Document.getElementById("search-location-selector-child")
AddressBox.Value = ActiveSheet.Range("B2")
Set html_doc = CreateObject("htmlfile")
Set xml_obj = CreateObject("MSXML2.XMLHTTP")
xml_obj.Open "GET", my_url, False
xml_obj.send
html_doc.body.innerHTML = xml_obj.responseText
Set SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter") 'id of the button control (HTML Control)
SearchButton.Click
While ie.ReadyState <> 4
DoEvents
Wend
I condensed your code a bit. You really do not need to set every element to a variable. This just wastes resources in the long run.
Use the ClassName submiter__text to grab your submit button, and it's index of 0.
Sub HGScrape()
Const sURL As String = "https://www.healthgrades.com/"
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate sURL
While .Busy Or .ReadyState < 4: DoEvents: Wend
.Document.getElementById("search-term-selector-child"). _
Value = ActiveSheet.Range("A2")
.Document.getElementById("search-location-selector-child"). _
Value = ActiveSheet.Range("B2")
.Document.getElementsByClassName("submiter__text")(0).Click
While .Busy Or .ReadyState < 4: DoEvents: Wend
End With
End Sub
"..Why was the "submitter_text" class the correct one?"
The best way to explain it is to show you. If you are unsure what selection to make, then right-click the element and choose "Inspect Element" and look around the highlighted line.

VBA Auto Login Web page and fetch the data

I am trying to login Web page and fetch data however my login details are not getting update, i have tried all possibilities code from your forum, nothing is working for me
Below is my code, am getting attached error
Sub test()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://portal.expeditors.com/expo/login"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "username" Then
objCollection(i).Value = "bom-sumand"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
I would use the available ids rather than looping to find the input boxes and sign in. These are much faster selector methods. You can add a .Focus. Also, swop InternetExplorer for InternetExplorerMeduim in some cases.
If problem continues check your internet settings in case site is blocked.
Open the URL via creating an IE instance direct.
Option Explicit
Public Sub Login()
Dim ie As New InternetExplorer 'InternetExplorerMedium
Const MAX_WAIT_SEC As Long = 5
Dim t As Date, ele As Object
With ie
.Visible = True
.navigate "https://portal.expeditors.com/expo/login"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
DoEvents
On Error Resume Next
Set ele = .getElementById("j_username")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
With ele
.Focus
.Value = "bob"
End With
With .getElementById("j_password")
.Focus
.Value = "penny"
End With
.getElementById("signInBtn").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me later
.Quit
End With
End Sub
Macro trying and failing to open a second instance of IE try this.
Sub test()
Dim ie As Object
Dim redURL As String
Dim objCollection As Object
Dim i As Integer
redURL = "https://portal.expeditors.com/expo/login"
On Error Resume Next
Set ie = GetObject(, "InternetExplorer.Application")
If Err Then
Set ie = CreateObject("InternetExplorer.Application")
End If
On Error GoTo 0
ie.Visible = True
ie.Navigate redURL
Do While ie.Busy
Loop
End Sub

Excel VBA Code Not Working With Internet Explorer 11

I have a vba code which can upload the data from an excel sheet to a website. However, the code works fine in IE browser 8,but it does not work on a win8 IE browser 11. It gives error 438 ("Object Does'nt support this property or method") and text field remains blank. Here are part of the code:
Dim ie As New InternetExplorer
Dim DOCS As HTMLDocument
ie.Navigate "http://uhs.edu.pk/results/etr2015.php"
ie.Visible = True
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
ie.Document.getElementsName("ROLLNO").Value = "55"
Need help to resolve this problem.
This Should Work for you.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub
If you also want to automate the serach button this should do the trick
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
'Clean up
Set ie = Nothing
End Sub
Code to also fetch data from new page
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Load the login page
ie.navigate "http://uhs.edu.pk/results/etr2015.php"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("input")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'Login name
If objCollection(i).Name = "rollno" Then
objCollection(i).Value = "55"
End If
'Store login button in object
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'Click login
objElement.Click
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set objCollection = ie.Document.getElementsByTagName("tr")
Range("A2").Value = Split(objCollection(23).innertext, "Sr. No.")(1)
Range("B2").Value = Split(objCollection(24).innertext, "Roll No.")(1)
Range("C2").Value = Split(objCollection(25).innertext, "Name of the Candidate")(1)
Range("D2").Value = Split(objCollection(26).innertext, "Father's Name ")(1)
Range("E2").Value = Split(objCollection(27).innertext, "Centre")(1)
Range("F2").Value = Split(objCollection(28).innertext, "Entrance Test Marks ")(1)
Range("G2").Value = Split(objCollection(29).innertext, "Total Marks")(1)
'Clean up
ie.Quit
Set ie = Nothing
End Sub
Hope this helps you out :)
Solution for combobox on the other page.
Sub testIE()
Dim ie As Object
Dim objCollection As Object
Dim objElement As Object
Dim i As Integer
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'Load the login page
ie.navigate "http://result.biselahore.com/"
'Wait until the page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Get all the elements with input tag name
Set objCollection = ie.Document.getElementsByTagName("Select")
i = 0
'Loop through all elements and find login form and fill it
While i < objCollection.Length
'either one of the methodes below works
'If objCollection(i).Name = "session" Then objCollection(i).Value = 2
If objCollection(i).Name = "session" Then objCollection(i).Item(2).Selected = True
If objCollection(i).Name = "year" Then objCollection(i).Item(2).Selected = True
i = i + 1
Wend
'Clean up
Set ie = Nothing
End Sub

Pull value from website (HTML div class) using Excel VBA

I'm trying to automate going to a website and pulling the ratings from several apps.
I've figured out how to navigate and login to the page.
How do I pull the element - the number "3.3" in this case - from this specific section into Excel.
Being unfamiliar with HTML in VBA, I got this far following tutorials/other questions.
Rating on website and the code behind it
Sub PullRating()
Dim HTMLDoc As HTMLDocument
Dim ie As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.appannie.com/account/login/xxxxxxxxxx"
Set ie = New InternetExplorer
ie.Silent = True
ie.navigate sURL
ie.Visible = True
Do
'Wait until the Browser is loaded
Loop Until ie.readyState = READYSTATE_COMPLETE
Set HTMLDoc = ie.Document
HTMLDoc.all.Email.Value = "xxxxxxxxx#xxx.com"
HTMLDoc.all.Password.Value = "xxxxx"
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Dim rating As Variant
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
Range("A1").Value = rating
'ie.Refresh 'Refresh if required
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The code below will let you extract text from first element with class name "rating-number ng-binding" in HTML document. By the way GetElementsByClassName is supported since IE 9.0. I use coding compatible also with older versions in my example.
Dim htmlEle1 as IHTMLElement
For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
If htmlEle1.className = "rating-number ng-binding" then
Range("A1").Value = htmlEle1.InnerText
Exit For
End if
Next htmlEle1
While Ryszards code should do the trick if you want to use the code you have already written then here is the alterations I believe you need to make.
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml