Automated Login Will Not Enter Credentials-Prompts Error - excel

I'm encountering an error when trying to automate the login process for Kronos. I'm able to get to the site, but it stops short of entering in my credentials.
Have looked thru old code that I have for reference, and tried to find a solution on Stack but have been unable to. I Referenced MS HTML Object Library, MS Internet Controls, & WebServices 1.0 Type Library.
Sub LoginToKronos()
Const cURL = "https://wikainstruments-sso.prd.mykronos.com/"
Const cUserID = "firstname.lastname#business.com"
Const cPassword = "Password123"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInputBox As HTMLInputElement
Dim SubmitButton As HTMLInputButtonElement
Dim PasswordInputBox As HTMLInputElement
Dim LogOnButton As HTMLInputButtonElement
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 Username textbox and populate it
'input name="loginfmt" id="i0116" value="" class="form-control ltr_override" type="email"
Set UsernameInputBox = doc.getElementById("i0116")
UsernameInputBox.Value = cUserID
'Get the form input button and click it
Set SubmitButton = doc.getElementById("idSIButton9")
SubmitButton.Click
'Get the Password textbox and populate it
'input name="Password" id="passwordInput" class="text fullWidth" type="password"
Set PasswordInputBox = doc.getElementById("passwordInput")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
Set LogOnButton = doc.getElementById("idSIButton9")
LogOnButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
'avoid any pop up
End Sub
would be nice to get have the credentials populate.

Related

Excel VBA - website is hiding element using script (MSXML)

I am trying to scrape email id from this website https://www.construction.co.uk/c/689533/3d-structural-detailing-services-limited but website is somehow hiding email from scraping bot.
Need help in updating VBA code to get email from website or let me know if you have any alternative to do this.
HTML of website is
<div class="compInfoDetail">
<script>document.write(emrp('ufsftb/cfotpo.tifuumfA4etet/dp/vl','construction.co.uk'));</script>
<a href="mailto:teresa.benson-shettle#3dsds.co.uk?subject=Contact from construction.co.uk">
teresa.benson-shettle#3dsds.co.uk
</a></div>
VBA Code is
Sub construction_uk_contact_xml()
Dim IE As MSXML2.XMLHTTP60
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLBody As MSHTML.HTMLBody
url = Selection.Value
Set IE = New MSXML2.XMLHTTP60
IE.Open "GET", url, False
IE.Send
While IE.ReadyState <> 4
DoEvents
Wend
Set HTMLDoc = New MSHTML.HTMLDocument
Set HTMLBody = HTMLDoc.body
HTMLBody.innerHTML = IE.responseText
Set infos = HTMLBody.getElementsByClassName("compInfoSection")
For Each info In infos
If info.getElementsByTagName("div")(0).innerText = "Email" Then ActiveCell.Offset(0, 1).Value = info.getElementsByTagName("div")(1).outerHTML
Next
Set IE = Nothing
End Sub

VBA Automated Login: URL Changes After First Use Resulting in Error

Code posted below. After alot of trying I managed to scrape together a macro that it will auto-log me into my bank account. If I try to run it a second time, the URL changes on me and I get an error message. If I clear my browsing history and close/re-open the file, it works again. I've tried setting my Internet Options to "Delete Browsing History on Exit". I also tried changing "Dim IE As InternetExplorer" to"Dim IE As InternetExplorerMedium" because I read somewhere that that might help. Something having to do with this being a secure site and it was redirecting me to a parent URL. Does anyone know what could be causing this and how I can fix it? I've asked around on the other forums but have had no luck with a response. Would greatly appreciate some help. Tks.
Note: I'm being redirected from https://chaseonline.chase.com/ to https://www.chase.com/
Have searched the internet looking for solutions. Have tried to get help on other forums but have been unsuccessful. Have tried fixing it myself but since I don't know what is the root cause, I don't know what to fix.
Sub Chase()
Const cURL = "https://chaseonline.chase.com/"
Const cUserID = "XXXXXX"
Const cPassword = "XXXXXX"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim LogOnButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable
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 Username textbox and populate it
'input name="UserID" id="UserID" size="18" value="" class="user-name" type="text"
Set UsernameInputBox = doc.getElementById("UserID")
UsernameInputBox.Value = cUserID
'Get the Password textbox and populate it
'input name="Password" id="Password" size="18" class="user-password" type="password"
Set PasswordInputBox = doc.getElementById("Password")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
Set LogOnButton = doc.getElementById("logon")
LogOnButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
success

VBA - IE automation - .getelements

I'm trying to enter text into an input field that has no ID. When I inspect the element I get this:
<input title="Affected Customer(s)" class="custom-input ng-pristine ng-invalid ng-invalid-required ng-touched" role="combobox" aria-expanded="false" aria-required="true" aria-owns="typeahead-78-1606" aria-autocomplete="list" required="" type="text" placeholder="Enter customer name, email, login ID or corporate ID" entity="incident" field-name="customer.loginId" potentially-required-field="" auto-focus="" typeahead="user as user.firstName + ' ' + user.lastName for user in getList('person', $viewValue)" typeahead-loading="person.isLoading" typeahead-wait-ms="500" typeahead-on-select="onCustomerSelect($model)" typeahead-template-url="views/create/custom-add-user-dropdown.html" typeahead-min-length="3" prevent-click-event="" ng-model="person.ngModel">
how do I reference this input field? I have tried many ways but they all fail. I think my understanding is lacking and hopefully a bit of help will get me on the right track. The code I have now that tries to locate this input field is:
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://ewpg-app-1041.hydro.mb.ca/ux/smart-it/#/create/incident"
'Navigate to URL
IE.navigate URL
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"
'click create new
'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.Document.getElementsByName("Affected Customer(s)")
'...get the href link and print it to the sheet in col C, row y
result = aEle
'repeat times the # of ele's we have in the collection
Next
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
The line "objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"" gives me the error Run-time error 424 - object expected.
Thank you for any help you can provide, this will probably be extremely easy but I'm just not understanding something basic obviously. The code above was taken from a various sites, seems to work fine. I had the for look working for testing using different logic.
Try Below Code instead :
getElementsByClassName("custom-input ng-pristine ng-invalid ng-invalid-required ng-touched")

Select Link on website after login with Excel 2013 VBA

I am trying to open a link on a webpage after logging in with VBA in excel. So far my code successfully opens the website and logs on. However, no examples I've seen in the forums seem to work for actually opening the next link.
HTML Code from the site:
<td class="nrmlFnt nWrap">
<a id="ctl00_wpm_ac_ac_rbk_ctl01_lnkBrokerageAccountName" oncontextmenu="return false;" href="javascript:__doPostBack('ctl00$wpm$ac$ac$rbk$ctl01$lnkBrokerageAccountName','')">Retirement</a>
</td>
While the innertext and ID are unique, I've been unable to select them.
I am new to VBA and may not have been implementing some of the solutions to similar problems quite right. Below is my code:
Dim HTMLdoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Private Sub btnUpdate_click()
'Create Variables for web browsing
Dim MyHTML_Element As IHTMLElement 'Elements to search for: textboxes, buttons etc...
Dim MyURL As String
'Dim IE As InternetExplorerMedium
'Clear errors to prevent from stopping code
On Error GoTo Err_Clear
'Input Desired webpage login URL
MyURL = "https://client.schwab.com/Login/SignOn/CustomerCenterLogin.aspx"
Set MyBrowser = New InternetExplorer 'Creates new browser session
MyBrowser.Silent = True 'Avoids Pop-ups
MyBrowser.navigate MyURL 'Navigates to URL
MyBrowser.Visible = True 'Opens browser window. Change to False to run w/o opening
'Wait for web page to fully load
Do
DoEvents
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
'Enter Username:
HTMLdoc.all.ctl00_WebPartManager1_CenterLogin_LoginUserControlId_txtLoginID.Value = "username"
'Enter Password:
HTMLdoc.all.txtpassword.Value = "password"
'Click Login button on webpage:
For Each MyHTML_Element In HTMLdoc.getElementsByTagName("a")
If MyHTML_Element.ID = "ctl00_WebPartManager1_CenterLogin_LoginUserControlId_btnLogin" Then MyHTML_Element.Click: Exit For
Next
'Wait for web page to fully load
Do
DoEvents
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
'Select Account to be displayed:
Set HTMLdoc = MyBrowser.document
Set e = HTMLdoc.getElementById("ctl00_wpm_ac_ac_rbk_ctl01_lnkBrokerageAccountName")
e.Click
'Wait for web page to fully load
Do
DoEvents
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
'Define Err_Clear Function:
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
I feel this should be something pretty simple like the following, but it hasn't worked:
Set MyHTML_Element= MyBrowser.Document.getElementByID("ctl00_wpm_ac_ac_rbk_ctl01_lnkBrokerageAccountName").getElementsByTagName("a")(0)
MyHTML_Element.Click
I have also tried the following with no success:
For Each MyHTML_Element In HTMLdoc.getElementsByTagName("a")
If MyHTML_Element.ID = "ctl00_wpm_ac_ac_rbk_ctl01_lnkBrokerageAccountName" Then MyHTML_Element.Click: Exit For
Next
The webpage loads, logs in and then... nothing happens.
So I got it to work finally. I apparently needed the webpage to finish loading and the simple "READYSTATE_COMPLETE" didn't cut it. So I ended up with this to make sure the page fully loaded:
With MyBrowser
Do While .Busy Or .readyState <> 4
DoEvents
Loop
End With
And then I used the following code to select the link:
Set HTMLdoc = MyBrowser.document
Set e = HTMLdoc.getElementById("ctl00_wpm_ac_ac_rbk_ctl01_lnkBrokerageAccountName")
e.Click
It finally goes to the correct page! Now I just need to pull the correct data... easy right?

VB Click Button Macro Note Working

I am trying to get the results of tracking a package on my screen. In Excel, I have this code, which successfully opens the IE browser and successfully types the tracking number into the input box. What doesn't work is the last step - button click. I get the runtime error 91 - object variable or with block variable not set. Thanks in advance.
Sub TrackUSPS()
Const cURL = "https://tools.usps.com/go/TrackConfirmAction!input.action" 'Enter the web address here
Const trackNum = "9405511899560005266920" 'Tracking number
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim TrackInputBox As HTMLInputElement
Dim TrackButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable
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 tracking form on the page
Set LoginForm = doc.forms(1)
'Get the tracking input box and populate it
'<input type="text" id="search-text" autocomplete="off" name="searchText" class="default" value="Search USPS.com or Track Packages" onclick="javascript:dojo.byId('search-text').value='';"/>
Set TrackInputBox = LoginForm.elements("search-text")
TrackInputBox.Value = trackNum
'Get the form input button and click it
'<input type="image" id="search-btn" src="/media/images/global/blank.gif" alt="Process Search" />
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
This works for me, replace
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
with
IE.document.getelementbyID("search-btn").Click

Resources