【问题标题】:Webbrowser control: Get element value and store it into a variableWebbrowser 控件:获取元素值并将其存储到变量中
【发布时间】:2010-03-26 23:43:16
【问题描述】:

Winform:网络浏览器控件

Web 浏览器在 html 表格中显示以下内容。

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

对于上面的示例,html 代码可能看起来像这样

<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>

.

我要获取元素值,标签右边的值。

最好的方法是什么?

(我可以使用 DOM 还是需要使用正则表达式对 html 代码进行分段?) .

【问题讨论】:

    标签: c# winforms webbrowser-control


    【解决方案1】:

    有不止一种方法可以做到这一点。

    例如,这对你有用吗?

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

    【讨论】:

    • 如何检查元素是否首先存在?因为它会抛出 NullRefExcep 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多