I am trying to build a VBA code to visit a website, input email address and password and click "log in" button. I have reached to the point email address and password is inputted, just not able to complete the code for hitting send button.
Below is the core code for VBA:
Function LogintoWebsite(uid As String, password As String) As Boolean
Dim objIE As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim useridF1d As MSHTML.HTMLInputElement 'email
Dim passwordFld As MSHTML.HTMLInputElement 'password
Dim SinginBt As MSHTML.HTMLInputElement 'signin
Set objIE = New SHDocVw.InternetExplorer
objIE.navigate URLstr
Do Until objIE.readyState = READYSTATE_COMPLETE
Loop
Set ieDoc = objIE.document
objIE.Visible = True
Set useridF1d = ieDoc.all.Item("email")
useridF1d.Value = uid
Set passwordFld = ieDoc.all.Item("password")
passwordFld.Value = password
Set SinginBt = ieDoc.all.Item("btn btn-primary m-t-2 btn-block") 'all.Item("btn btn-primary m-t-2 btn-block")
SinginBt.Click
Do Until objIE.readyState = READYSTATE_COMPLETE
Loop
LogintoTransferwise = True
Set objIE = Nothing
Exit Function
And below is the HTML Element for the button:
<button class="btn btn-primary m-t-2 btn-block" type="submit">
Try,
Dim btn As Object
For Each btn In ieDoc.GetElementsByTagName("button")
If InStr(1, btn.InnerText, "Log in", vbTextCompare) > 0 Then
btn.Click
Exit For
End If
Next btn
in place of the lines
Set SinginBt = ieDoc.all.Item("btn btn-primary m-t-2 btn-block") 'all.Item("btn btn-primary m-t-2 btn-block")
SinginBt.Click
(Not Tested)
Related
I want to catch the anchor element in website via vba.
I tried both as an MSHTML.IHTMLAnchorElement and MSHMTL.IHTMLBUttonELement but either way gives an error on Set line. Can anyone describe what's wrong with code below?
Sub goToWhtSpp()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://cooperatordirectory.com/"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
Dim doc As MSHTML.HTMLDocument
Set doc = ie.document
Application.Wait 3000
URL = "https://cooperatordirectory.com/search_results?location_value=Florida&adm_lvl_1_sn=FL&stateSearchLN=Florida&country_sn=US&location_type=administrative_area_level_1&stateSearch=FL&swlat=24.396308&nelat=31.000968&swlng=-87.634896&nelng=-79.974306&lat=27.6648274&lng=-81.5157535&faddress=Florida%2C+USA&place_id=ChIJvypWkWV2wYgR0E7HW9MTLvc"
ie.navigate URL
Dim aelement As MSHTML.IHTMLAnchorElement
Set aelement = doc.getElementsByClassName("btn btn-primary btn-block") ' err line
Dim btn As MSHTML.IHTMLButtonElement
Set btn = doc.getElementsByClassName("btn btn-primary btn-block") ' err line
Debug.Print btn.Value
End Sub
<a class="btn btn-primary btn-block" href="/pro/20220324090906/connect">Send Message</a>
You want a page load wait after each .navigate. In this case you only need the one .navigate and wait.
getElementsByClassName() returns a collection not a single node. So both your type declaration, subsequent SET attempt to this type, and later attempt to view the .value attribute will fail. .value is a property of a single node.
If you want the first "button" href then index into a correctly typed variable and call .item(0), or, more efficiently, use the querySelector() method of ie.document and pass in a single class of the multi-valued class (the one which still correctly identifies the target element).
Dim anchors As IHTMLElementCollection
Set anchors = .document.getElementsByClassName("btn-primary")
Dim anchor As IHTMLAnchorElement
Set anchor = .document.getElementsByClassName("btn-primary").Item(0)
Option Explicit
Public Sub PrintMessageUrl()
Const URL As String = "https://cooperatordirectory.com/search_results?location_value=Florida&adm_lvl_1_sn=FL&stateSearchLN=Florida&country_sn=US&location_type=administrative_area_level_1&stateSearch=FL&swlat=24.396308&nelat=31.000968&swlng=-87.634896&nelng=-79.974306&lat=27.6648274&lng=-81.5157535&faddress=Florida%2C+USA&place_id=ChIJvypWkWV2wYgR0E7HW9MTLvc"
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate URL
Do While .readyState <> READYSTATE_COMPLETE Or .Busy: DoEvents: Loop
Debug.Print .document.querySelector(".btn-primary").href
.Quit
End With
End Sub
I am trying to work on a page thats already opened, I have managed to find and interact with the window, but I cant seem click on the button the search button. See code below:
Dim ele As Object
Set idoc = ie.Document
Dim eles As mshtml.IHTMLElementCollection
Set eles = idoc.getElementsByTagName("button")
Set objshell = CreateObject("shell.application")
intwincnt = objshell.Windows.Count
For inTWinNo = 0 To (intwincnt - 1)
strwintitle = objshell.Windows(inTWinNo).Document.Title
If strwintitle = "Articles" Then
Set ie = objshell.Windows(inTWinNo).Document
Exit For
End If
Next
ie.getElementsByName("vin")(0).Value = "test"
Set ele = ie.Document.getElementsById("vin-button")
ele.Click
<button class="btn btn-primary" id="vin-utton"type="submit">DECODE</button>
Morning guys,
Using already answered questions on here i am still unable to get any code to work for my particular site, unfortunately as it is an internal website i cannot share the actual site.
The site is a .jsp login page could this be causing the issue? The macro opens the login page in IE but doesn't enter any user data into the fields or submit currently.
I also get error message "the object invoked has disconnected from it's clients" once the macro is running.
Microsoft internet controls is active and
Forms 2.0 is active in references also
I am fairly new to VBA to if i am missing anything obvious please let me know, i have lowered security settings within IE also.
EDIT I am somewhat sure this is to do with my form ID's but i can't see where i went wrong?
Sub GetTable()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "http://dss-gp-mida-002/MidasDS/login.jsp
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.Username.Value = "Carterr"
.Password.Value = "password"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
ieApp.Navigate "http://dss-gp-mida-002/MidasDS/homepage.jsp"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.all.Item("sampletable")
'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If
'close 'er up
ieApp.Quit
Set ieApp = Nothing
End Sub
Page Source:
<input type="hidden" name="urlwanted" value=""><input type="hidden" name="sourcePageURL" value=""><input type="hidden" name="ssoiid" value="">
<div>
<div class="form-group">
<label for="username">Username</label><input name="username" id="username" type="text" class="form-control" placeholder="Enter Username">
</div>
<div class="form-group">
<label for="password">Password</label><input name="password" id="password" type="password" class="form-control" placeholder="Enter Password">
</div>
<script>
$("#username").keyup(function(event) {
if (event.which == 13) {
$("#loginform").submit();
}
});
$("#password").keyup(function(event) {
if (event.which == 13) {
$("#loginform").submit();
}
});
</script>
<div class="form-group">
<div class="btn-group-vertical-justified">
<button name="login" type="button" value="Log In" class="btn btn-primary" onclick="document.loginform.ssoiid.value="";document.loginform.submit()">Log In</button>
</div>
I used the following in something similar:
Set ie = GetIE("www.url.com")
If Not ie is Nothing Then
ie.navigate (strtarget) 'strtarget is the target url
Do Until ie.ReadyState = 4: DoEvents: Loop
If ie.LocationURL = "www.url.com/loginpage" Then
MsgBox "Login Please!!"
Else
pageText = ie.document.body.innertext
End if
Else
MsgBox "IE not found!!"
End if
It will display a messagebox which asks you to sign in if you land on the loginpage. Then you manually log in and start the script again.
I have this code that log in on instagram page. It catches the tag name of the username field and input a value on it (the username of the instagram I wanna log).
Code of Instagram page:
input name="username" class="_kp5f7 _qy55y" aria-required="true" aria-describedby="" aria-label="Nome de usuário" type="text" maxlength="30" placeholder="Nome de usuário" value="" autocapitalize="off" autocorrect="off"
Full Code (edited by the user, can't edit post)
Sub FOLLOW()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim contador, contador2, contador3, lngremoto, turnopar As Integer
Dim numeropages, xopepacounter, usuario, senha, numeroseguir, tipopage As String
Dim numeropaginasseguir, entrarpage As String
usuario = Cells(2, 2).Value
senha = Cells(2, 3).Value
'abrir o IE
IE.Visible = True
apiShowWindow IE.Hwnd, SW_MAXIMIZE
'maximizar, caso minimizado
IE.navigate ("www.instagram.com")
'Wait for ie to LOAD
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Sleep 1500
'Click on switch accounts
'Set inputcollection = IE.document.getElementsByTagName("a")
'For Each aelement In inputcollection
' If aelement.getAttribute("class") = "_niffh" Then
'If InStr(aelement.innerText, "Trocar de conta") Then
'aelement.Click
'End If
'End If
'Next
'Click on login button
Set inputcollection = IE.document.getElementsByTagName("a")
For Each aelement In inputcollection
If aelement.getAttribute("class") = "_fcn8k" Then
If InStr(aelement.innerText, "Fa?a login") Then
aelement.Click
End If
End If
Next
'Wait for IE to load
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Sleep 3000
'Fill the field username
Set inputcollection = IE.document.getElementsByTagName("input")
For Each aelement In inputcollection
If aelement.getAttribute("name") = "username" Then
aelement.Focus
aelement.Value = usuario
Sleep 1000
End If
Next
'Fill the field password
Set inputcollection = IE.document.getElementsByTagName("input")
For Each aelement In inputcollection
If aelement.getAttribute("name") = "password" Then
aelement.Focus
aelement.Value = senha
Sleep 1000
End If
Next
'click on sign in
Set inputcollection = IE.document.getElementsByTagName("button")
For Each aelement In inputcollection
If aelement.getAttribute("class") = "_ah57t _84y62 _7xso1 _rmr7s" Then
If InStr(aelement.innerText, "Entrar") Then
aelement.Click
End If
End If
Next
End Sub
The problem is that the code isn't filling the field correctly. Visually it looks like its filled on Instagram page, but when you click to log in a message error appear saying that I didn't got the username field filled.
This code is only working with the send keys method, and it does not work for me cause I want to hide the Internet Explorer window.
Any suggestions?
Hello I am writing code to automatically log onto CIBC investors edge and my code only opens of the cibc investors page. I have a feeling it is a problem with how I am calling the username and password values. Below is the general code and the element snapshots from the inspected elements on CIBC.
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyInvEdge()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://www.investorsedge.cibc.com/ie/"
Set MyBrowser = New InternetExplorer
Myrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.userID.Value = "my ID"
HTMLDoc.all.userPassword.Value = "my password"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "Sign On" Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The HTML code for both elements is the following:
<input name="userID" tabindex="3" id="userID" onkeydown="keyPressedEvt
(event,'newUserID');" type="text" maxlength="25" autocomplete="off">
<input name="userPassword" tabindex="4" id="userPassword" type="password"
maxlength="12" autocomplete="off">
Please help me sort out this sign in issue.
Thanks,
I think this:
HTMLDoc.all.userID.Value = "my ID"
should be something like
HTMLDoc.GetElementByID("userID").Value ="my ID"
(same for password)
Also, use DoEvents to pass control back to the processor when waiting for the page to load:
So something like
While MyBrowser.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
instead of
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Finally, I'm not sure "Sign On" is a HTML element, so using .Type won't work here:
MyHTML_Element.Type = "Sign On"
But you haven't shown the HTML for that...