Consulting

Results 1 to 5 of 5

Thread: Using VBA to Log In to a Wesbite

  1. #1

    Using VBA to Log In to a Wesbite

    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!

    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.

    HTML Code:
    <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"/>

    HTML Code:
    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
    Last edited by DavidLee91; 06-30-2015 at 03:45 AM.

  2. #2
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    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

  3. #3
    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.

  4. #4
    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/...d.php?t=232898

  5. #5
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •