Results 1 to 11 of 11

Thread: [RESOLVED]Help with clicking login through webBrowser

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    [RESOLVED]Help with clicking login through webBrowser

    Here is the source
    HTML Code:
    <td rowspan=2> </td><td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_email.gif"></td> 
    <td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName"  size="25"></td> 
    <td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_password.gif"></td> 
    <td><input  class="Data_Entry_Field_Login"  type="password" name="inUserPass" id="inUserPass"></td> 
    <td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Part 1: Use WebBrowser control to load web page
            WebBrowser1.Navigate("http://www.locationary.com")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            ' Part 2: Username and password
            Dim theElementCollection As HtmlElementCollection
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In theElementCollection
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "inUserName" Then
                    curElement.SetAttribute("Value", "myUsername")
                ElseIf controlName = "inUserPass" Then
                    curElement.SetAttribute("Value", "myPassword")
                End If
            Next
            WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
        End Sub
    End Class
    Everything works fine except the login, it gives me an "Object reference not set to an instance of an object" error.
    Last edited by Datadayne; Aug 26th, 2010 at 01:32 PM.

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Help with clicking login through webBrowser

    thanks
    amrita

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: Help with clicking login through webBrowser

    Thanks for the reply, but im already using whats suggested in that link
    Code:
    WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
    Can someone tell me what the tagName for this button is
    HTML Code:
    <td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Help with clicking login through webBrowser

    using your html snippet +

    vb Code:
    1. WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")

    it clicked the button as expected. can you post the full html?



    Quote Originally Posted by Datadayne View Post
    Can someone tell me what the tagName for this button is
    HTML Code:
    <td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
    the tagName is "input"

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: Help with clicking login through webBrowser

    Thanks for the reply, full html is too long to post, but here is the part of the html that includes the login:

    HTML Code:
    <form name="TopLoginForm" method="post" action="https://www.locationary.com/web/login/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction" id="m_Login"> 
    <div style="float:right;"> 
     
    <table  border=0> 
     
    <tr> 
     
    <td rowspan=2> </td><td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_email.gif"></td> 
    <td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName"  size="25"></td> 
    <td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_password.gif"></td> 
    <td><input  class="Data_Entry_Field_Login"  type="password" name="inUserPass" id="inUserPass"></td> 
    <td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr> 
     
    <tr><td></td><td ></td><td></td><td><a  class="Medium " href="http://www.locationary.com/web/login/forgotpwd.jsp">Forgot Password?</a> 
     
    </td><td></td></tr> 
    </table> 
    </div> 
     
    </form>

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Help with clicking login through webBrowser

    it worked with that too...

    put this in a button + click it:

    vb Code:
    1. for each element as htmlelement in webbrowser1.document.getelementsbytagname("input")
    2.     msgbox(element.getattribute("id"))
    3. next

    does the submit button show up?

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: Help with clicking login through webBrowser

    Looks like it, Im getting inUserName, inPassName and login@DEFAULT.

    Edit:
    I tried adding this in the button click rather than in the webBrowser and its working for some odd reason.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Part 1: Use WebBrowser control to load web page
            WebBrowser1.Navigate("http://www.locationary.com")
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            ' Part 2: Username and password
            Dim theElementCollection As HtmlElementCollection
            theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In theElementCollection
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "inUserName" Then
                    curElement.SetAttribute("Value", "myUser")
                ElseIf controlName = "inUserPass" Then
                    curElement.SetAttribute("Value", "myPass")
                End If
            Next
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
        End Sub
    End Class
    Any reasons why its working if I plug it in the button rather than the webBrowser.
    Last edited by Datadayne; Aug 25th, 2010 at 09:49 PM.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Help with clicking login through webBrowser

    the WebBrowser1_DocumentCompleted event fires more than once when navigating to a page. for an accurate measure of when the page is loaded check webbrowser1.readystate in your WebBrowser1_DocumentCompleted event

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: Help with clicking login through webBrowser

    You mean like this?
    vb Code:
    1. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    2.         ' Part 2: Username and password
    3.         Dim theElementCollection As HtmlElementCollection
    4.         theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
    5.         For Each curElement As HtmlElement In theElementCollection
    6.             Dim controlName As String = curElement.GetAttribute("name").ToString
    7.             If controlName = "inUserName" Then
    8.                 curElement.SetAttribute("Value", "user")
    9.             ElseIf controlName = "inUserPass" Then
    10.                 curElement.SetAttribute("Value", "pass")
    11.             End If
    12.         Next
    13.        If WebBrowserReadyState.Complete Then
    14.             WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
    15.         Else : MsgBox("WebBrowser did not complete")
    16.         End If
    17.     End Sub
    The msgbox doesnt display so it should be completed, I switched wbreadystate.complete with .uninitialized and as expected the msgbox did display.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Help with clicking login through webBrowser

    that's how i meant, although you should put the other code inside the 'If WebBrowserReadyState.Complete Then' block too.

    i'm guessing it didn't work, or you wouldn't be asking?

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    57

    Re: Help with clicking login through webBrowser

    Well switching the id of the login with the value seems to have worked.
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         ' Part 1: Use WebBrowser control to load web page
    5.         WebBrowser1.Navigate("http://www.locationary.com")
    6.     End Sub
    7.  
    8.     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    9.         ' Part 2: Username and password
    10.         Dim theElementCollection As HtmlElementCollection
    11.         theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
    12.         For Each curElement As HtmlElement In theElementCollection
    13.             Dim controlName As String = curElement.GetAttribute("name").ToString
    14.             If controlName = "inUserName" Then
    15.                 curElement.SetAttribute("Value", "user")
    16.             ElseIf controlName = "inUserPass" Then
    17.                 curElement.SetAttribute("Value", "pass")
    18.             End If
    19.         Next
    20.         theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
    21.         For Each curElement As HtmlElement In theElementCollection
    22.             Dim controlValue As String = curElement.GetAttribute("Value").ToString
    23.             If controlValue = "Submit" Then
    24.                 curElement.InvokeMember("click")
    25.             End If
    26.         Next
    27.     End Sub
    Although I still dont know why using the id doesnt work. Thanks for your time and help .paul.

Posting Permissions

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



Click Here to Expand Forum to Full Width