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 AsString = "https://login.my_site_here.jsp"Const userName AsString = "Here Your LogIn Name"Const passWord AsString = "Here Your Password"Dim ie AsObjectDim htmlDoc AsObjectDim nodeInputUserName AsObjectDim nodeInputPassWord AsObject'Initialize Internet Explorer, set visibility,'call URL and wait until page is fully loadedSet ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate url
  DoUntil ie.readyState = 4: DoEvents: LoopSet htmlDoc = ie.document
  
  'Set the log in nameSet nodeInputUserName = htmlDoc.getElementById("USERID")
  nodeInputUserName.Value = userName
  Call TriggerEvent(htmlDoc, nodeInputUserName, "onkeypress")
  
  'Set the passwordSet nodeInputPassWord = htmlDoc.getElementById("PASSWORD")
  nodeInputPassWord.Value = passWord
  Call TriggerEvent(htmlDoc, nodeInputPassWord, "onkeypress")
  
  'Click submit button
  htmlDoc.querySelector("a[role='button']").Click
  
EndSub

This is the procedure to trigger events:

PrivateSub TriggerEvent(htmlDocument AsObject, htmlElementWithEvent AsObject, eventType AsString)

  Dim theEvent AsObject

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

Post a Comment for "Excel Vba To Login To Website And Grab Tr & Td Elements"