Skip to content Skip to sidebar Skip to footer

Excel VBA To Login To Website And Grab TR & TD Elements

I am testing the code below. I think this is very close, but I can't seem to login to the site for some reason. Sub Website_Login_Test() Dim oHTML_Element As IHTMLElement Dim sUR

Solution 1:

I think you must trigger the keypress event of the input fields. If there are other events you must trigger, have a look here, how you can find them:
Automate IE via Excel to fill in a dropdown and continue

Sub WebsiteLogin()

Const url As String = "https://login.my_site_here.jsp"
Const userName As String = "Here Your LogIn Name"
Const passWord As String = "Here Your Password"

Dim ie As Object
Dim htmlDoc As Object
Dim nodeInputUserName As Object
Dim nodeInputPassWord As Object

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate url
  Do Until ie.readyState = 4: DoEvents: Loop
  Set htmlDoc = ie.document
  
  'Set the log in name
  Set nodeInputUserName = htmlDoc.getElementById("USERID")
  nodeInputUserName.Value = userName
  Call TriggerEvent(htmlDoc, nodeInputUserName, "onkeypress")
  
  'Set the password
  Set nodeInputPassWord = htmlDoc.getElementById("PASSWORD")
  nodeInputPassWord.Value = passWord
  Call TriggerEvent(htmlDoc, nodeInputPassWord, "onkeypress")
  
  'Click submit button
  htmlDoc.querySelector("a[role='button']").Click
  
End Sub

This is the procedure to trigger events:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)

  Dim theEvent As Object

  htmlElementWithEvent.Focus
  Set theEvent = htmlDocument.createEvent("HTMLEvents")
  theEvent.initEvent eventType, True, False
  htmlElementWithEvent.dispatchEvent theEvent
End Sub

Post a Comment for "Excel VBA To Login To Website And Grab TR & TD Elements"