如何在WebBrowser控件C#中查找特定单词

时间:2014-01-14 08:24:30

标签: c# browser mshtml

我使用此代码在HTML中查找CDE。如何在标签页中找到具有不同ID的请求数据,例如,如果页面ID为 1 ,则结果为 CDE ,且页面ID为 2 我的结果是 IJK ,如何在搜索中设置id-value?

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText = @"<html><head><title></title></head><body>" 
                               + "<page id=\"1\">"
                               + @"ABCDEF</page>"
                               + "<page id=\"2\">"
                               + @"GHIJKLMN</page></body></html>";

    webBrowser1.DocumentCompleted += HtmlEditorDocumentCompleted;
}

void HtmlEditorDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var document = (IHTMLDocument2)((WebBrowser)sender).Document.DomDocument;

    if (document != null)
    {
        IHTMLBodyElement bodyElement = document.body as IHTMLBodyElement;

        if (bodyElement != null)
        {
            IHTMLTxtRange trg = bodyElement.createTextRange();

            if (trg != null)
            {
                trg.move("character", 2);
                trg.moveEnd("character", 3);
                trg.select();
                trg.pasteHTML("<font color=#FF0000><strike>" + trg.text + "</strike></font>");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道,你需要做什么,你可能需要重新解释这个问题......

但您可能希望看一下webBrowser1.Document.GetElementDyId()来获取特定元素&#34; id&#34;