Pushing a button on webpage via vba - excel

So my overall goal is to scrape data from a table that is behind a security wall. I have done the code to launch IE and input the security creditals however the code I have for clicking the Submit button isn't working. The webpage I am working with is:
https://www.myfueltanksolutions.com/validate.asp
I have put the code below:
Private Sub CommandButton1_Click()
Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True
i.navigate ("https://www.myfueltanksolutions.com/validate.asp")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
idoc.all.CompanyID.Value = "CompanyID"
idoc.all.UserId.Value = "UserID"
idoc.all.Password.Value = "Password"
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Set eles = idoc.getElementsByTagName("button")
For Each ele In eles
If ele.Name = "btnSubmit" Then
ele.Click
Else
End If
Next ele
End Sub

btnSubmit is not a button, it is an image.
Private Sub CommandButton1_Click()
Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True
i.navigate ("https://www.myfueltanksolutions.com/validate.asp")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
idoc.all.CompanyID.Value = "CompanyID"
idoc.all.UserId.Value = "UserID"
idoc.all.Password.Value = "Password"
Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
'Set eles = idoc.getElementsByTagName("button")
Set eles = idoc.getElementsByTagName("img")
For Each ele In eles
If ele.Name = "btnSubmit" Then
'ele.Click
ele.parentNode.Click ' click the <a> that contains this element
Else
End If
Next ele
End Sub
The submit button is just calling a javascript function, so you could skip finding the button all togeather.
idoc.parentWindow.execScript "submitForm();"

Related

How To Click On Web Page Image Link?

I tried several different methods to select a radio button or click on the image. There is an onclick task that is to be triggered. I can't automate any further until this opens up.
Sub AirCan()
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLButton As MSHTML.IHTMLElement
Dim doc As MSHTML.HTMLDocument
Dim img As MSHTML.HTMLImg
On Error GoTo Err_Clear
MyURL = "https://XXXXXXXXXX.com/en/XXXXXXXXXXXXX"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.paymentAmount.Value = "111" 'Enter your email id here
HTMLDoc.all.groupPayment.click
Do While MyBrowser.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set allInputs = HTMLDoc.getElementsByTagName("img")
For Each element In allInputs
If element.getAttribute("src") = "/HPPDP/hpp/images/ca_visa.png" Then
element.click
Exit For
End If
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
CODE from the website, clicking the radio button or any of the 3 images will do what I need,

"object doesn't support property or method" VBA login

I'm trying to login in this website using VBA but no success so far.
Option Explicit
Sub logintest()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "www.renovigi.solar/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("loginusr") ' This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("loginpwd") 'This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("loginbtn") 'This is based on on your website
HTMLButton.Click
End Sub
I'm getting this error message.
"object doesn't support property or method"
You need to get the input elements and then set the values. also, you may try the alternative way which I have commented.
Sub logintest()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "www.renovigi.solar/"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("loginusr").Children(0)
'Set HTMLInput = HTMLDoc.querySelector("#loginusr input") ' This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("loginpow").Children(0)
'Set HTMLInput = HTMLDoc.querySelector("#loginpow input") 'This is based on on your website
HTMLInput.Value = "TEST" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("loginbtn") 'This is based on on your website
HTMLButton.Click
End Sub

Unable to click on webpage based on attribute results

I am trying to navigate in "https://www.ford.co.uk/shop/price-and-locate/build-and-price-gf3#/catalogID/WAEGB-CE1-2015-B479FiestaCarGBR201950/?code=". I have to select Titanium in the model list. But i am not able to select it. Everytime the list of models will vary based on link.
Below is the code which i have tried.
Sub testcode()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "https://www.ford.co.uk/shop/price-and-locate/build-and-price-gf3#/catalogID/WAEGB-CE1-2015-B479FiestaCarGBR201950/?code="
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:03"))
Set htmldoc = ie.document
Set HTMLAs = htmldoc.getElementsByClassName("features-list-item feature-box")
For Each HTMLA In HTMLAs
Debug.Print HTMLA.getAttribute("id"), HTMLA.getAttribute("tabindex")
If HTMLA.getAttribute("id") = "feature-1" And HTMLA.getAttribute("tabindex") = 0 Then
HTMLA.Click
End If
Next HTMLA
Set HTMLAs = Nothing
Set HTMLA = Nothing
End Sub
The result should be webpage loaded and selected the titanium series. Kindly assist me achieve this.
HTMLA itself doesn't seem to have a click handler, but you can click on one of its child elements.
Try this:
If HTMLA.getAttribute("id") = "feature-1" And HTMLA.getAttribute("tabindex") = 0 Then
HTMLA.getElementsByTagName("img")(0).Click
Exit For
End If

How to login a website using VBA and retrieve data to excel?

There are plenty of examples of how to login into a website, like gmail
But for some reason my code only opens the webpage without logging into the target page, using the provided username and password
I've enabled the HTML object library and Microsoft Internet Controls
Any thoughts , as I am fairly new to this object library?
Plus, how do I grab a table to import into excel from a website after I log in?
I tried macro recording but it needs a password to login into the website
An example would be helpful :) Thanks so much!
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyGmail()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.google.com/accounts/Login"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.Navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document
HTMLDoc.all.Email.Value = "nasterpizza87#gmail.com"
HTMLDoc.all.passwd.Value = "************" 'my password would be here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”)
If MyHTML_Element.Type = “submit” Then _
MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
Sub LoginToSite()
Const cURL = "https://singlepoint.usbank.com/cs70_banking/logon/sbuser"
Const ccompanyID = "YourCustomerIDHere"
Const cj_username = "YourUserIDHere"
Const cj_password = "YourPasswordHere"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim CustomerIDInputBox As HTMLInputElement
Dim UserIDInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim objElement As Object
Dim objCollection As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document
'Get the only form on the page
Set LoginForm = doc.forms(0)
'Get the CustomerID textbox and populate it
'input name="companyID" id="companyID" size="18" value="" type="text"
Set CustomerIDInputBox = doc.getElementById("companyID")
UsernameInputBox.Value = ccompanyID
'Get the UserID textbox and populate it
'input name="j_username" id="j_username" size="18" type="text"
Set UserIDInputBox = doc.getElementById("j_username")
UsernameInputBox.Value = cj_username
'Get the Password textbox and populate it
'input name="j_password" id="j_password" size="18" type="password"
Set PasswordInputBox = doc.getElementById("j_password")
PasswordInputBox.Value = cj_password
'Get the form input button and click it
'input name="submit_logon" size="18" type="image"
Set ElementCol = IE.document.getElementsByName("submit_logon")
ElementCol.Item(0).Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
If it's specifically Gmail that you're trying to login to then the below code will work. I use it.
Sub Gmail()
'Set a reference (Visual Basic Editor) > Tools > References) to the following libraries:
' a) Microsoft Internet Controls
' b) Microsoft HTML Object Library
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLdoc As New MSHTML.HTMLDocument
Dim HTMLelement As MSHTML.IHTMLElement
With IE
.Visible = True
.Silent = True 'avoid any pop-up
.navigate "https://www.google.com/accounts/Login"""
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Call myTimer
Set HTMLdoc = IE.document
HTMLdoc.all.identifier.Value = "YourUsernameHere"
HTMLdoc.all.identifierNext.Click
With IE
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Call myTimer
For Each HTMLelement In HTMLdoc.getElementsByName("password")
If HTMLelement.getAttribute("type") = "password" Then
HTMLelement.Value = "YourPasswordHere"
Exit For
End If
Next HTMLelement
HTMLdoc.all.passwordNext.Click
Set IE = Nothing
Set HTMLdoc = Nothing
Set HTMLelement = Nothing
End Sub
Private Sub myTimer()
Dim timerStart As Single
Const pauseTIME As Integer = 1 'second
timerStart = Timer
Do Until Timer - timerStart > pauseTIME
DoEvents
Loop
End Sub

Using vba to login

I want to use vba to login to a site. This makes it easy for everyone and not everyone has to know the password this way.
However, the site was recently updated and now the code that I used (that was duct taped together from bits and pieces)
`Sub apiweb()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://apiweb.biomerieux.com/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.login.Value = "xxxx"
HTMLDoc.all.Password.Value = "yyyy"
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub`
So it used to work that you press a button in excel, the site would open and after a second or 2 the login and password would show up and you'd just press login. Now it only opens the site.
I tried a few things including inspecting the elements, but i'm a novice so I don't really know what to look for or what to do. So any help (and explanation) would be appreciated!
How about this method?
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://apiweb.biomerieux.com/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.signupEmail.Value = "signupEmail"
HTMLDoc.all.signupPassword.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("signupSubmit")
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
As an aside, hit F12 to see the HTML behind your browser.
Option Explicit
Sub WbClkMe()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "https://apiweb.biomerieux.com/login"
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
Set HTMLInput = HTMLDoc.getElementById("signupEmail") ' This is based on on your website
HTMLInput.Value = "" 'Put the value of Usernamae
Set HTMLInput = HTMLDoc.getElementById("signupPassword") 'This is based on on your website
HTMLInput.Value = "" 'Put the value of Password
Set HTMLButton = HTMLDoc.getElementById("signupSubmit") 'This is based on on your website
HTMLButton.Click
End Sub

Resources