Internet scraping issue when using vba on secure site - vba

I am trying to automate IE to pull data from a secure site how every I keep getting the same error message: "Object required"
The debugger points to the line -HTMLinput.Value = "test"
Everything before works fine. I don't know what's the problem. I have verified my ID to make sure there is no mistakes.
Sub Brows()
Dim IE As New SHDocVw.InternetExplorerMedium
Dim Policy As Object
Dim certificate As Object
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLinput As MSHTML.IHTMLElement
IE.Visible = True
IE.navigate "secure website address"
Application.Wait Now + TimeValue("00:00:02")
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLinput = HTMLDoc.getElementById("input ID")
HTMLinput.Value = "test"
End Sub

How to log into a web site using Excel and VBA.
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
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

Related

Href VBA clicking

I've been trying many codes to click the right button and open the page to scrape the data.
I was able to have the form filled and then click on submit, but the second part I can't seem to put to work. I want it to click on the href with has the same name that I searched on the form
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub CETIP()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www2.cetip.com.br/TitulosCRI"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.Navigate sURL
oBrowser.Visible = True
Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.document
HTMLDoc.all.ddlCodigoIF.Value = "08L0002118"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Err_Clear:
Resume Next
End Sub

Login to website using VB Macro

I am trying to pull data from a site using Macro. Below is my code. It fills up the ID and password, but does not actually log into the site. Need help to identify where is the actual error. then I can slowly work on going into the right location to extract the report.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub HRALogin()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "website" 'removed actual website due to sensitivity
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.UserName.Value = "abcdefg"
HTMLDoc.all.Password.Value = "1234abcd"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then
oHTML_Element.Click
End If
Exit For
Next
Do
'Wait till the Browser is loaded
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
Also, set the delay after you click on login, so that it loads the page correctly and then do the rest stuff.
This:
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then oHTML_Element.Click: Exit For
Next
is equivalent to this:
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "login" Then
oHTML_Element.Click
End If
Exit For
Next
i.e. the Exit For is not controlled by the If. That might be your problem.

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:
<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>
Is there a way to reference this button?
Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!
Make sure you update ie.navigate to your site.
This is a late binding example, you can also set a reference to Microsoft Internet Controls.
Sub clickButton()
Dim ie As Object
Dim Btn As Object
'Late Binding
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo Catch
ie.Visible = True
ie.navigate "https://yourwebsite.com/"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
'LOOP EACH CLASS ELEMENT
For Each Btn In ie.Document.getElementsByClassName("button")
If Btn.innertext = "Edit" Then
Btn.Click
End If
Next Btn
'CLOSE INSTANCE OF IE
Catch:
ie.Quit
Set ie = Nothing
End Sub
Here is a very generic sample of how to do this.
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
Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

Interacting with a web page using VBA

I just want to know the following:
How do I login to a pages using VBA?
How do I click on various options in a web page using VBA?
How do I enter text into required forms in a web page using a VBA?
Here is a classic example of how to do all of those things.
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
Make sure to set references to:
Microsoft HTML Object library
&
Microsoft Internet Controls

How to use VBA to click on button in webpage after you have logged in?

I am trying to click on a button on a webpage that I login to with VBA. I have already logged in and now I want to continue the process and click on another button. Here is what I have so far
Sub MyGmail()
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.programworkshop.com/6.0.0.0/login/login.aspx?skin=default&lang=enu"
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.Login.value = "something#something.com"
HTMLDoc.all.pass.value = "Password"
HTMLDoc.all.btnlogin.Click
This next line is where I hit a dead end
HTMLDoc.all.sd5.Click
Here is the code from the website
<a id="sd5" class="nodeSel" href="javascript:treeAction(strURL5)" onmouseover="window.status='Examinees';return true;" onmouseout="window.status='';return true;" onclick="javascript: d.s(5);">Examinees</a>
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
HTMLDoc.all.btnlogin.Click
While MyBrowser.Busy
DoEvents
Wend
Set HTMLDoc = MyBrowser.Document
HTMLDoc.all.sd5.Click