Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to log-in to a website through code. For that I have to submit username and password in website's log-in page by Code only.
I have write the following code.

VB
Private Sub WebBrowserEx1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowserEx1.DocumentCompleted

  Dim theElementCollection As HtmlElementCollection = WebBrowserEx1.Document.GetElementsByTagName("INPUT")

   For Each curElement As HtmlElement In theElementCollection
            Dim controlName As String = curElement.GetAttribute("name").ToString

            If controlName.ToString.Contains("Username") Then
                curElement.SetAttribute("uttam", controlName)
            ElseIf controlName.ToString.Contains("Password") Then
                curElement.SetAttribute("test", controlName)
               exit For
            End If

     Next

        ' Part 3: Automatically click the Login button

        Dim theWElementCollection As HtmlElementCollection = WebBrowserEx1.Document.GetElementsByTagName("INPUT")

        For Each curElement2 As HtmlElement In theWElementCollection
            Dim controlName2 As String = ""
            controlName2 = curElement2.GetAttribute("NAME").ToString()
            If controlName2.Contains("Action") Then
                curElement2.InvokeMember("Click")
                Exit Sub
            End If
        Next

        WebBrowserEx1.Navigate("http://dev.koliu.in/Main.aspx")
        Exit Sub
End Sub

But although I have passed the username and password I am unable to see next page of my website.
What could be the problem? Please help.
Posted
Updated 22-Oct-10 23:51pm
v2

1 solution

First of all, remove all your exit fors and exit subs because they are modifying the intended flow. For example, if the password control is found before the username, your code will simply not work. There is no need to optimize until your code actually works.

Comment out the navigate, then put a breakpoint on InvokeMember line to see if it ever gets called.

I hope this is of some help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900