开发者

Webbrowser control: Get element value and store it into a variable

开发者 https://www.devze.com 2022-12-25 00:36 出处:网络
Winform: Web browser Control The web browser has the following displayed content, within a html table.

Winform: Web browser Control

The web browser has the following displayed content, within a html table.

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

For the example above, 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><l开发者_运维技巧abel class="label">Email</label></td>
        <td><span class="normaltext">jsmith@hotmail.com</span></td>
</tr>
    </tr>
    </tbody>
</table>

.

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

What is the best way to do this?

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


there're more than one way to accomplish this.

For instance, will 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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号