Reputation: 41
I continue to get the above error when running this code. I have tried dozens of web searches and adjustments to fix but unsuccessful. The code is below and any help if very much appreciated.
Public Sub Tagg()
Dim URL As String
Dim ie As SHDocVw.InternetExplorer 'MICROSOFT Internet Controls (shdocvw.dll)
Dim HTMLdoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim loginFrame As HTMLIFrame
Dim usernameInput As HTMLInputElement, passwordInput As HTMLInputElement
Dim username As String, password As String
username = "MTorres" 'CHANGE THIS
password = "melissa1" 'CHANGE THIS
URL = "https://webaccess.tagglogistics.com/cadence/webaccess.net?action=203&Full=Y&args=415878"
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate URL
Do While .readyState <> READYSTATE_COMPLETE Or .Busy
DoEvents
Loop
Set loginFrame = .document.getElementById("loginframe") ' ****ERROR HERE****
Set HTMLdoc = loginFrame.contentWindow.document
'<input name="username" id="uname" class="ti" maxlength="32" onchange="setUserValue();"
'onkeydown="setupUserValue(event);" onmouseup="return false;" onclick="SelectAll();" onfocus="SelectAll();"
'aria-describedby="cof_username errormsg" type="text">
Set usernameInput = HTMLdoc.getElementsByName("username")(0)
usernameInput.Focus
DoEvents
usernameInput.Value = username
usernameInput.FireEvent "onkeydown"
usernameInput.FireEvent "onchange"
usernameInput.FireEvent "onmouseup"
'<input id="cofisso_ti_passw" name="password" class="ti" maxlength="32" aria-describedby="pass" type="password">
Set passwordInput = HTMLdoc.getElementsByName("password")(0)
passwordInput.Focus
passwordInput.Value = password
'HTMLdoc.forms(0).submit
'<input src="/resources/images/btn_login.gif" alt="Login" title="Login" name="cofisso_btn_login" id="cofisso_btn_login" type="image">
HTMLdoc.getElementById("cofisso_btn_login").Click
Do While .readyState <> READYSTATE_COMPLETE Or .Busy
DoEvents
Loop
'----------- NEW CODE --------------
'Might need this wait loop
While .document.readyState <> "complete"
DoEvents
Wend
'Either reload HTMLdoc from current IE.document:
Set HTMLdoc = .document
'Or if LNKLOGOUT is inside an iframe:
' Dim iframe As HTMLIFrame
' Set iframe = .document.getElementsByTagName("IFRAME")(0) '0 = 1st iframe
' Set HTMLdoc = iframe.contentWindow.document
'HTMLdoc should now be available here - display webpage TEXT TO verify
MsgBox HTMLdoc.body.innerText
'---------- END OF NEW CODE ----------
'Click "Sign Out" link
'<a id="LNKLOGOUT" class="logout" href="https://servicing.capitalone.com/C1/SelfService/CMLogoutIntercept.aspx">Sign Out</a>
HTMLdoc.getElementById("LNKLOGOUT").Click
Do While .readyState <> READYSTATE_COMPLETE Or .Busy
DoEvents
Loop
End With
End Sub
Upvotes: 3
Views: 3363
Reputation: 84465
You could have shortened most of that by using CSS selectors to target the elements of interest.
The selectors are:
input[name='LoginID']
input[name='Password']
input[type='image']
These says select elements with input
tag having attribute name
or type
, with value of 'Login'
or 'Password'
or 'image'
respectively. "[]" is the selector for attribute.
VBA:
You apply CSS selectors with .querySelector
method of document
:
.document.querySelector("input[name='LoginID']").Value = USERNAME
.document.querySelector("input[name='Password']").Value = PASSWORD
.document.querySelector("input[type='image']").Click
Code:
Option Explicit
Public Sub Tagg()
Dim URL As String, ie As SHDocVw.InternetExplorer, HTMLdoc As MSHTML.HTMLDocument
Const USERNAME As String = "MTorres"
Const PASSWORD As String = "melissa1"
URL = "https://webaccess.tagglogistics.com/cadence/webaccess.net?action=203&Full=Y&args=415878"
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate URL
Do While .readyState <> READYSTATE_COMPLETE Or .Busy
DoEvents
Loop
.document.querySelector("input[name='LoginID']").Value = USERNAME
.document.querySelector("input[name='Password']").Value = PASSWORD
.document.querySelector("input[type='image']").Click
Do While .readyState <> READYSTATE_COMPLETE Or .Busy
DoEvents
Loop
Set HTMLdoc = .document
'Other code
.Quit
End With
End Sub
Upvotes: 1
Reputation: 22338
You aren't setting the document and loginframe correctly.
Replace these lines:
Set loginFrame = .document.getElementById("loginframe") ' ****ERROR HERE****
Set HTMLdoc = loginFrame.contentWindow.document
With these:
Set HTMLdoc = ie.Document
Set loginFrame = HTMLdoc.getElementById("loginframe") ' ****ERROR FIXED****
You'll get another error a few lines down when you try to find an element with the name "username" because there is no such element on that page, but hopefully this will get you on the right track.
Upvotes: 0