none
Web Browser Control C#.Net

Answers

  • Hi Hari,

     

    Welcome to MSDN Forum.

     

    The detailed instruction and solution are provided in this FAQ. Please check it.

    FAQ: How do I automate a web page (e.g. retrieve page text, login website, search, click button or hyperlink) via WebBrowser?

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/23dfc3f9-3274-4929-8dc5-9ba543f4911d

     

    Basic principle:

    First load a web page on a WebBrowser object, then use the GetElementsByTagName or GetElementsByID function to locate the webpage elements in WebBrowser.Document and automate them (e.g. retrieve page text, login website, search, click button or hyperlink).

    Beforehand, you need to find out the web elements’ source html code via view source (Right-click on web page -> "View Source" menu item).

     

    For example: How do I automatically login to website (input uername/password and click Login button)?

    Assume that this page http://www.website.com/login.aspx has the following elements:

    <input name="UserNameTextBox" type="text" value="myUser" id="UserNameTextBox">

    <input name="PasswordTextBox" type="text" value="myUser" id="PasswordTextBox">

    <INPUT type=submit value="Login" name="LoginButton">

     

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            ' Part 1: Use WebBrowser control to load web page

            WebBrowser1.Navigate("http://www.website.com/login.aspx")

     

            System.Threading.Thread.Sleep(2000) ' Delay 2 seconds to render login page

     

            ' Part 2: Automatically input 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 = "UserNameTextBox" Then

                    curElement.SetAttribute("Value", "Username text here")

                ElseIf controlName = "PasswordTextBox" Then

                    curElement.SetAttribute("Value", "Password text here")

                    'In addition,you can get element value like this:

                    'MessageBox.Show(curElement.GetAttribute("Value"))

                End If

            Next

     

            ' Part 3: Automatically clck that Login button

            theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")

            For Each curElement As HtmlElement In theElementCollection

                If curElement.GetAttribute("value").Equals("Login") Then

                    curElement.InvokeMember("click")

                    ' javascript has a click method for you need to invoke on button and hyperlink elements.

                End If

            Next

        End Sub

    End Class

     

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Zain_Ali Friday, August 12, 2011 4:44 AM
    • Marked as answer by Martin_XieModerator Monday, August 15, 2011 3:06 AM
    Friday, August 12, 2011 2:32 AM