Link to home
Start Free TrialLog in
Avatar of investalert
investalertFlag for United States of America

asked on

Login to my favorite web site

Looking for code to login to my favorite https:// web site using my userID and PW. I am experienced in vba and VB6, but new to vs2010 and ASP.NET. I've been studying these newer programming techniques, but need help in getting started in ASP.NET. I can find the tags in my web site using IE8 and F12 to search or outline the DOMs and pick out the userID, PW, and submit if button omitted by using innerText. Your help is greatly appreciated.
Note: I've tried other code snippets found on the web but they appear to be for earlier versions of .NET and give me errors when pasted in VS2010.
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

Are you using the WebBrowser component?
Avatar of investalert

ASKER

I can but,... I have struggled with building a Windows Form Application orASP.NET Web Application in VB 2010. I believe it is in my best interest to build it in ASP.NET using Web Application for future expansion. However, I am open to suggestions.
This is how i've done my login in the past, using a WebBrowser.  You probably can do that without putting the object on a form...

 
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        If Not bSendPasswordPostBack Then
            Dim sUserNameField As String = "ctl00_ContentPlaceHolder1_UserName"
            Dim sUserPasswordField As String = "ctl00_ContentPlaceHolder1_Password"
            Dim sSubmitButton As String = "ctl00_ContentPlaceHolder1_btnLogin"

            bSendPasswordPostBack = True
            WebBrowser1.Document.GetElementById(sUserNameField).InnerText = "MyUserName"
            WebBrowser1.Document.GetElementById(sUserPasswordField).InnerText = "MyPassword"
            WebBrowser1.Document.GetElementById(sSubmitButton).InvokeMember("click")
        Else
            bSendPasswordPostBack = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sMyWebSite As String = "https://yourwebsite.com"

        WebBrowser1.Navigate(sMyWebSite)
    End Sub

Open in new window


What is the web site that you are trying to access?  Because you say that you can't find the tags for the login, but they must be somewhere...
Note: bSendPasswordPostBack is a boolean to make sure that you won't fill up the username & password on 2nd DocumentCompleted.   I've not shown the declaration so here it is:

Private bSendPasswordPostBack As Boolean
Thanks of the quick reply. I tried it in VS2010 and get the following:

bSendPasswordPostBack is noted with error message "Expression is a value and therefore cannot be the target of an assignment" in If Statement.

And Yes, I have the tags for the userID and PW. They are XXXXNUM and XXXXPass.  

 The HTML is shown below.
<DIV id="logonBox">
<FORM id="Logon" onsubmit="return handleLogonSubmit()" method="post" name="Logon" action="j_security_check">
<INPUT id="fp_syslang" type="hidden" name="fp_syslang" value="" /> <INPUT id="fp_software" type="hidden" name="fp_software" value="" /> <INPUT id="fp_userlang" type="hidden" name="fp_userlang" value="" /> <INPUT id="fp_display" type="hidden" name="fp_display" value="" /> <INPUT id="fp_lang" type="hidden" name="fp_lang" value="" /> <INPUT id="fp_timezone" type="hidden" name="fp_timezone" value="" /> <INPUT id="fp_browser" type="hidden" name="fp_browser" value="" /> <INPUT type="hidden" name="LogonToken" value="" /> <INPUT type="hidden" name="PageType" value="" /> <LABEL id="XXXXNumLabel" for="XXXXNum">Online ID</LABEL> <INPUT onkeydown="ChangeFocus(event);" id="XXXXNum" class="loginField" onkeypress="removeErrorMessageDiv()" aria-labelledby="messageLoginErrorLabel XXXXNumLabel" aria-required="true" maxLength="20" size="25" name="j_username" value="" /> <LABEL for="XXXXPass">Password</LABEL> <INPUT id="XXXXPass" class="loginField" onkeypress="removeErrorMessageDiv()" aria-required="true" maxLength="12" size="25" type="password" name="j_password" value="" /><BUTTON class="nAction" onclick="false" type="submit">Log On</BUTTON> 
<P class="textSmall">
Forgot Your <A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=forgotOnlineId">Online ID</A> or <A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=forgotPassword">Password</A>?
</P>
<P class="textSmall">
<A href="https://www.XXXX.com/inet/ent_proof/proofingEvent?action=Init&event=registration"><STRONG>Register with XXXX.com</STRONG></A>
</P>
</FORM>
</DIV>

Open in new window

By the way, I did add the webBrowser control to my Form, if that makes any difference.
Sorry for that, as i said, i forgot to include the declaration for bSendPasswordPostBack.
In this sample, my form name is Form1 as you can see.

 
Public Class Form1
    Private bSendPasswordPostBack As Boolean

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        If Not bSendPasswordPostBack Then
            Dim sUserNameField As String = "ctl00_ContentPlaceHolder1_UserName"
            Dim sUserPasswordField As String = "ctl00_ContentPlaceHolder1_Password"
            Dim sSubmitButton As String = "ctl00_ContentPlaceHolder1_btnLogin"

            bSendPasswordPostBack = True
            WebBrowser1.Document.GetElementById(sUserNameField).InnerText = "MyUserName"
            WebBrowser1.Document.GetElementById(sUserPasswordField).InnerText = "MyPassword"
            WebBrowser1.Document.GetElementById(sSubmitButton).InvokeMember("click")
        Else
            bSendPasswordPostBack = False
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sMyWebSite As String = "https://yourwebsite.com"

        bSendPasswordPostBack = False
        WebBrowser1.Navigate(sMyWebSite)
    End Sub
End Class

Open in new window

Thanks for the reply. Your code goes to the web site and enters the userID and PW. Need to go to meeting and will look at the detail later and respond. Thanks again cedebel!
OK, got my user name and password entered in the boxes as needed. However, the submit button does not have an ID. I entered the code below and it works, but it causes a circular execution meaning that it enters a continous loop and I don't know how to stop it. Also, when I manually click the submit button on the web page, the web page shows it is trying to navigate to the next page but hangs up. What else do I need to do?
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
        For Each webpageelement As HtmlElement In allelements
            If webpageelement.InnerText = "Log On" Then
                webpageelement.InvokeMember("click")
            End If

Open in new window

Here is the next web page I need to go to:

 https://www.XXXX.com/inet/ent_logon/Logon
It appears the "bSendPasswordPostBack = False" is causing a Log Off and then the page reloads and enters the UserID and Password. This is a loop that continues until the close button is clicked or the Stop Debugging is pressed.

The logon page where the UserID and Password are entered is shown below:

https://www.xxxx.com/inet/ent_logon/Logon

The page to navigate to after the logon is complete is:

https://www.XXXX.com/inet/ent_home/CpHome
ASKER CERTIFIED SOLUTION
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've a meeting to prepare, so i'll be back in 2h.
OK, got all the bugs worked out and web page logged on and navigate to correct page. Thanks for your help.
Had to feness that coding and sequence to get final results. However, expert's guidance was good based on information I provided, which was somewhat limited due to security of my web site.