Web browser control: get element value and store it in a variable

Winform: Web Browser Control

The web browser has the following displayable content in the html table.

[Element]   [Value]
Name        John Smith
Email       jsmith@hotmail.com

      

In the above example, the html code might look something like this:

<table>
    <tbody>
    <tr>

        <td><label class="label">Name</label></td>
        <td class="normaltext">John Smith</td>
    </tr>
    <tr>    <td><label class="label">Email</label></td>
        <td><span class="normaltext">jsmith@hotmail.com</span></td>
</tr>
    </tr>
    </tbody>
</table>

      

...

I want to get the value of an element, the value to the right of the label.

What's the best way to do this?

(Can I use the DOM or do I need to phase the html code with a regex?).

+1


source to share


1 answer


there are several ways to do this.

For example, would this work for you?



using System.Windows.Forms;

namespace TestWebBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            webBrowser1.DocumentText = @"<html><body><table>
    <tbody>
    <tr>

        <td><label class=""label"">Name</label></td>
        <td class=""normaltext"">John Smith</td>
    </tr>
    <tr>    <td><label class=""label"">Email</label></td>
        <td><span class=""normaltext"" id=""e1"">jsmith@hotmail.com</span></td>
</tr>
    </tr>
    </tbody>
</table>
</body>
</html>";
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            HtmlElement e1 = webBrowser1.Document.GetElementById("e1");
            MessageBox.Show(e1.InnerText);
        }
    }
}

      

+3


source







All Articles