Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

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

Winform: Web browser Control

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

[Element]   [Value]
Name        John Smith
Email       [email protected]

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">[email protected]</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?) .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"">[email protected]</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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...