I wrote a macro so I could fill a form and access a website automatically:
Private Sub CommandButton1_Click()
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "EXAMPLE.COM"
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.USER.Value = "USER1"
HTMLDoc.all.USER.FireEvent ("onchange")
HTMLDoc.all.PASS.Value = "PASS"
HTMLDoc.all.PASS.FireEvent ("onchange")
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("img")
If MyHTML_Element.getAttribute("src") = "imagesrc.gif" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
After the page loads, I've been trying to get it to click another button with no luck. Do you have any idea for this?
Here is how to pause your program so that it can wait for the webpage to load. There are multiple ways of doing this but this little line of code works wonders.
Application.Wait(Now + TimeValue("0:00:10"))
Just copy and paste that into your code after the click even. This will wait for approximately 10 seconds. You can adjust the time as needed.
Related
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
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.
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.
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
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