How to automate login to a web site.

evel: Intermediate
How to automate login to a web site.
Login automation - Internet Explorer & Web Browser Control
Sometimes, we need to login to gain access to complete site. I will show you a way to log to www.hotmail.com, which requires user id, password and to push a button.
Before go further showing code, we need to know how login process takes place.
Sites needs to know where you enter things and we need too. Generally, textboxes used to enter id and password have names to diferentiate among others textboxes and it is marvelous. We need those names too.

Those names are located inside HTML code. We have to "read" HTML code the first time we access login page.

To this time, i already did that. The textbox name for user id is: login and textbox name for password is: passwd. The name of the button we need to press is: enter.

By now, you already know how to set object variables for InternetExplorer and you have been using WebBrowser control, so, i will not explain here again.

Private Sub Form_Load()
set IE = New InternetExplorer
With IE
.navigate "www.hotmail.com"
.visible = True
End With
End Sub

Let's go to DocumentComplete event:

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
'Url could change so take care of it ;)
If InStr(1, URL, "passport.com/cgi-bin/login", vbTextCompare) Then
Dim inp As Object
Set inp = IE.document.all.Item("login")
inp.Value = "myname" 'replace with your id

Set inp = IE.document.all.Item("passwd")
inp.Value = "passwrd" 'replace with your password

Set inp = IE.document.all.Item("enter")
inp.Click
End if
End If
End Sub

A variation:

Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IE) Then
'Url could change so take care of it ;)
If InStr(1, URL, "passport.com/cgi-bin/login", vbTextCompare) Then
IE.document.all.Item("login").value = "myname" 'replace with your id

IE.document.all.Item("passwd").Value = "passwrd" 'replace with your password

IE.document.all.Item("enter").Click
End if
End If
End Sub

No comments:

Ramadan - What is it?

  Ramadan is one of the most important and holy months in the Islamic calendar. It is a time of fasting, prayer, and spiritual reflection fo...