PDA

View Full Version : Using VBA to Log In to a Wesbite



DavidLee91
06-29-2015, 11:12 PM
Hi Folks!

I am currently trying to get a VBA code to log into a website. However, I am facing an error and would really appreciate if anybody can help me! :bow:

The website I am currently trying to log in is: http://www.questnet.sg/

Below is the VBA code with Error:


Function LogInl()

Set objIEBrowser = CreateObject("InternetExplorer.Application")

objIEBrowser.Visible = True

objIEBrowser.Navigate2 "http://www.questnet.sg/"

Call FnWaitForPageLoad(objIEBrowser)

Set objPage = objIEBrowser.Document

Call FnLogin(objPage, "davidlee91", "password")

Call FnWaitForPageLoad(objIEBrowser)

End Function

'----------------------------------------------------------------------------------------------------------------------

Function FnLogin(objPage, strUserName, strPwd)

Dim Name As String

Set NameEditB = objPage.getElementByID("txtUserID")

Set PWDEditB = objPage.getElementByID("txtPassword")

NameEditB.Value = strUserName

PWDEditB.Value = strPwd


For Each objPage_Element In objPage.getElementsByTagName("input")
If objPage_Element.Type = "login" Then objPage_Element.Click:
Exit For
Next

' Set SignIn = objPage.getElementByID("signIn")

' SignIn.Click

End Function



The error message is: Run-time error '91' - Object Variable or With block variable not set.

The highlighted line in VBA is:

NameEditB.Value = strUserName

The bottom code is a part of the HTML of the website which I used to get the ElementByID.



<tr><td class="fieldlabel_bluebig">LogIn Name</td></tr>
<tr><td><input class="big_textfield" name="txtUserID" size="25" onKeyPress="javascript:enterLogin(event);" autocomplete="off"/>


<tr><td class="fieldlabel_bluebig">Password</td></tr>
<tr><td>]<input class="big_textfield" name="txtPassword" type="password" size="25" onKeyPress="javascript:enterLogin(event);" autocomplete="off"/>





function validEntry() {
if (document.forms["frmLogin"].txtUserID.value != "" && document.forms["frmLogin"].txtPassword.value != "" ){
return true ;
}else {
alert("Please key in user name and password to login!")
return false;
}
}


Guys, thanks for reading this lengthy post and I would really appreciate any help!

Thanksss!!

Regards,
David

mohanvijay
06-30-2015, 01:25 AM
Hi Davidlee,

The "Username" and "Password" elements in HTML don't have the "Id" attribute. They have only name.
you have to use the following code to get those elements


Set NameEditB = objPage.getElementsByName("txtUserID").item(0)


If more than one elements have the same name then you can use "getElementsByTagName" to get all "input" elements
and use some looping to get correct element

DavidLee91
06-30-2015, 01:39 AM
Hi Mohanvijay,

Thank you for your reply!

I amended the code, however, I still face the same error and VBA highlight this code:


NameEditB.Value = strUserName

I would really appreciate if you could let me know what went wrong again. :banghead:

DavidLee91
06-30-2015, 03:18 AM
Hi Guys,

I have been googled the whole afternoon and it seems like the possible reasons was due to the forms used in the website. I have no idea but does anybody know how I can amend this into my code?


HTMLDoc2.forms("FORMNAME").TEXTFIELDNAME.Value

Credit: http://www.access-programmers.co.uk/forums/showthread.php?t=232898

DavidLee91
06-30-2015, 03:43 AM
Hi Guys,

I tried to research and found a new code that tried to look for forms. However, it is still not working! Apparently, the code just determined that there is no forms in the website and exit the FOR loop. Any help would be appreciated.


ption Explicit
Sub IE_login()
Dim ie As InternetExplorer
Dim C
Dim ULogin As Boolean, ieForm
Dim MyPass As String, MyLogin As String

redo:

MyLogin = "David"
MyPass = "password"


If MyLogin = "" Or MyPass = "" Then GoTo redo

Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "http://www.questnet.sg/"

'Loop until ie page is fully loaded
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop

'Look for password Form by finding test "Password"
For Each ieForm In ie.document.forms
If InStr(ieForm.innerText, "txtPassword") <> 0 Then
ULogin = True
'enter details
ieForm(0).Value = MyLogin
ieForm(2).Value = MyPass
'login
ieForm.submit
Exit For
Else
End If
Next
If ULogin = False Then MsgBox "User is aleady logged in"
Set ie = Nothing
End Sub

Credit: VBA Express Knowledge Base