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

送分小仙女□ 提交于 2019-12-17 16:41:48

问题


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><label 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?) .


回答1:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/2527499/webbrowser-control-get-element-value-and-store-it-into-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!