locked
WebBrowser Fill In Form Fields RRS feed

  • Question

  • Well the WebBrowser control...

    Is there a way of filling in elements within a html page.

    So basically i wan't a button which when pressed will fill any form text boxes (within the WebBrowser page obviously) with the name of "emailadd" (<input type="text" name="emailadd" value="" />) with an email address which has already been supplied.

     

     

     

     

    And also is there a way of submitting this form for users to login automatically.

     

    Thnx,

    Tarun

    Monday, September 10, 2007 4:00 PM

Answers

  •  

    Hi Tarun,

    The HtmlElement has a SetAttribute Method to set the value of the named attribute on the element. We can get the element first and use this method to set the value. To submit a form, the HtmlElement also provide you a InvokeMember Method that executes a method unique to the current element. Check my sample below:

    Code Snippet

    namespace WebBrowser

    {

        public partial class WBSubmit : Form

        {

            public WBSubmit()

            {

                InitializeComponent();

            }

     

            private void WBSubmit_Load(object sender, EventArgs e)

            {

                this.webBrowser1.Navigate(@"C:\test.htm");

                this.button1.Click += new EventHandler(button1_Click);

            }

     

            void button1_Click(object sender, EventArgs e)

            {

                HtmlDocument htmlDoc = this.webBrowser1.Document;

                HtmlElement txtElement = htmlDoc.All["emailadd"];

                if (txtElement != null)

                {

                    txtElement.SetAttribute("value", "microsoft@microsoft.com");

                }

                HtmlElement btnElement = htmlDoc.GetElementById("submit");

                if (btnElement != null)

                {

                    btnElement.InvokeMember("Click");

                }

            }

        }

    }

     

    Html code for test.htm

    Code Snippet

    <html>

    <title>test</title>

    <head>

    <script language="javascript" type="text/javascript">

    function showText()

    {

     alert("Submit!");

    }

    </script>

    </head>

    <body>

    <input name="emailadd" type="text" maxLength="25" size="82" value=""/>

    <input type="submit" id="submit" value="submite" onclick="showText()"/>

    </body>

    </html> 

     

    Best regards.
    Rong-Chun Zhang

    Wednesday, September 12, 2007 4:49 AM

All replies

  •  

    Hi Tarun,

    The HtmlElement has a SetAttribute Method to set the value of the named attribute on the element. We can get the element first and use this method to set the value. To submit a form, the HtmlElement also provide you a InvokeMember Method that executes a method unique to the current element. Check my sample below:

    Code Snippet

    namespace WebBrowser

    {

        public partial class WBSubmit : Form

        {

            public WBSubmit()

            {

                InitializeComponent();

            }

     

            private void WBSubmit_Load(object sender, EventArgs e)

            {

                this.webBrowser1.Navigate(@"C:\test.htm");

                this.button1.Click += new EventHandler(button1_Click);

            }

     

            void button1_Click(object sender, EventArgs e)

            {

                HtmlDocument htmlDoc = this.webBrowser1.Document;

                HtmlElement txtElement = htmlDoc.All["emailadd"];

                if (txtElement != null)

                {

                    txtElement.SetAttribute("value", "microsoft@microsoft.com");

                }

                HtmlElement btnElement = htmlDoc.GetElementById("submit");

                if (btnElement != null)

                {

                    btnElement.InvokeMember("Click");

                }

            }

        }

    }

     

    Html code for test.htm

    Code Snippet

    <html>

    <title>test</title>

    <head>

    <script language="javascript" type="text/javascript">

    function showText()

    {

     alert("Submit!");

    }

    </script>

    </head>

    <body>

    <input name="emailadd" type="text" maxLength="25" size="82" value=""/>

    <input type="submit" id="submit" value="submite" onclick="showText()"/>

    </body>

    </html> 

     

    Best regards.
    Rong-Chun Zhang

    Wednesday, September 12, 2007 4:49 AM
  • Hi,

    How to select a check box present in the navigated page using WebBrowser Control.
    <ul id="categorychecklist">
    <li id="category-3"><label for="in-category-3" class="selectit"><input value="3" type="checkbox" name="post_category[]" id="in-category-3"/> General</label></li>
    <li id="category-5"><label for="in-category-5" class="selectit"><input value="5" type="checkbox" name="post_category[]" id="in-category-5"/> Media</label></li>
    <li id="category-4"><label for="in-category-4" class="selectit"><input value="4" type="checkbox" name="post_category[]" id="in-category-4"/> Personal</label></li>
    <li id="category-7"><label for="in-category-7" class="selectit"><input value="7" type="checkbox" name="post_category[]" id="in-category-7"/> Quote of the day</label></li>
    <li id="category-6"><label for="in-category-6" class="selectit"><input value="6" type="checkbox" name="post_category[]" id="in-category-6"/> Software</label></li>
    <li id="category-1"><label for="in-category-1" class="selectit"><input value="1" type="checkbox" name="post_category[]" id="in-category-1"/> Uncategorized</label></li>
    </ul>
    The navigated page has list of check boxes with the html above.
    I want to select a particular check box say "category-7".
    How do I do this using webbrowser control.

    Thanks
    tamilG
    Sunday, September 23, 2007 5:37 PM