Logon to website using VBA

Tosborn

New Member
Joined
May 24, 2016
Messages
44
Logon to website using VBA

Hey all,

Trying to log in to a website with VBA & download a file.

Having troubles getting the code to login.

I’ve pulled up what the element is in IE but the code doesn’t seem to recognise it and no username or password gets put in at all.

Code is below, in particular the .elements part is not functioning

Code:
Option Explicit
Private oBrowser As InternetExplorer

Private Sub openBrowserAndLogin()
 Set oBrowser = New InternetExplorer

 With oBrowser
  .Visible = True
  .navigate "https://www.cuscal.com.au/opensso/UI/Login"
  
  
  Do While .Busy Or .readyState <> READYSTATE_COMPLETE
   DoEvents
  Loop

  On Error Resume Next
  With .document.forms("LoginForm")
    .elements("IDToken1").Value = "username"
    .elements("password").Value = "Password"
   .submit
  End With
  On Error GoTo 0

  Do While .Busy Or .readyState <> READYSTATE_COMPLETE
   DoEvents
  Loop
 End With
End Sub

Private Function takeSnapshot() As String
 Dim oTables As IHTMLElementCollection
 Dim oTable As IHTMLElement
 Dim sTableHTML As String
 With oBrowser
  Set oTables = .document.getElementById("fleetGrid").getElementsByTagName("table")
  Set oTable = oTables(1)
  sTableHTML = oTable.innerHTML
 End With
 takeSnapshot = sTableHTML
End Function

Private Sub getWebContentOnTime()
 Dim oHTMLDoc As IHTMLDocument
 Dim oTable As IHTMLElement
 Dim oTR As IHTMLTableRow
 Dim oCell As IHTMLTableCell
 Dim oWS As Worksheet
 Dim oClip As DataObject
 Dim sTableHTML As String
 Dim sDivClassName As String
 Dim aClassProps As Variant
 Dim dTime As Double
 Dim lRows As Long
 Dim lCols As Long
 Dim lColsRow As Long


 sTableHTML = takeSnapshot()

 Set oHTMLDoc = New HTMLDocument
 oHTMLDoc.body.innerHTML = "<html><table id=""t1"">" & sTableHTML & "</table></html>"

 Set oTable = oHTMLDoc.getElementById("t1")
 lRows = 0
 lCols = 0
 For Each oTR In oTable.Rows
  lColsRow = 0
  For Each oCell In oTR.Cells
   sDivClassName = oCell.FirstChild.className
   aClassProps = Split(sDivClassName, "_")
   If aClassProps(0) = "fleet" Then
    On Error Resume Next
    oCell.Style.backgroundColor = aClassProps(1)
    oCell.Style.Color = aClassProps(2)
    On Error GoTo 0
   End If
   lColsRow = lColsRow + 1
  Next
  If lColsRow > lCols Then lCols = lColsRow
  lRows = lRows + 1
 Next

 Set oClip = New DataObject
 oClip.SetText "<html><table>" & oTable.innerHTML & "</table></html>"
 oClip.PutInClipboard

 Set oWS = ThisWorkbook.Worksheets(1)
 oWS.Paste Destination:=oWS.Range(oWS.Cells(1, 1), oWS.Cells(lRows, lCols))

 dTime = Now + TimeSerial(0, 0, 5)
 Application.OnTime EarliestTime:=dTime, _
        Procedure:="getWebContentOnTime", _
        Schedule:=True

End Sub

Public Sub getWebContentMain()
 Dim dTime As Double

 Call openBrowserAndLogin

 dTime = Now + TimeSerial(0, 0, 10)
 Application.OnTime EarliestTime:=dTime, _
        Procedure:="getWebContentOnTime", _
        Schedule:=True

End Sub



Any ideas on how I can fix?


Thanks so much.

Tim
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Replace the With .document.forms() .... End With block with:
Code:
  .document.getElementById("IDToken1").Value = "username"
  .document.getElementById("IDToken2").Value = "password"
  .document.getElementsByName("Login.Submit")(0).Click
 
Upvote 0
Hi Sir,

:confused: I have below sorce. I want to click on Send SMS Button (way2sms.com)
But i am not able to do this from long time. Please need your help.


Source :
<div class="mainmenu">
<div id="smsMENU" class="wrap" style="display: block">
<ul class="ovh">
<li id="sdash" class="home active" *******="javascript:loadSMSPage('sdash');"><i class="chati homei"><em class="ei"></em></i></li>
<li id="sendSMS" class=""><a href="javascript:loadSMSPage('sendSMS');">Send SMS</a></li>
<li id="langSMS"><a href="javascript:loadSMSPage('langSMS');">Language SMS</a></li>
<li id="groupSMS"><a href="javascript:loadSMSPage('groupSMS');">Group SMS</a></li>
<li id="sentSMS"><a href="javascript:loadSMSPage('sentSMS');">Sent SMS</a></li>
<li id="aBook"><a href="javascript:loadSMSPage('aBook');">Address Book</a></li>
<li id="smsGreets"><a href="javascript:loadSMSPage('smsGreets');">SMS Greetings</a></li>
<li id="futureSMS"><a href="javascript:loadSMSPage('futureSMS');">Future SMS</a></li>
<li id="smartphone"> <a href="https://goo.gl/gs5BFW" target="blank">
<i class="new"></i><span class="way2sms-bubble">Personal Loans</span></a>
</li>
<li id="usersettings" style="display: none;"><a href="javascript:loadSMSPage('usersettings');">Settings</a></li>
<li id="ychats" style="display: none;"><a href="javascript:getdata('menusettings','listchathistory','ychats');">Chat History</a></li>
<li id="gchats" style="display: none;"><a href="javascript:getdata('menusettings','listchathistory','gchats');">Chat History</a></li>
</ul>


</div>
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top