单击带有webbrowser c#的链接

时间:2011-12-25 16:35:19

标签: c# webbrowser-control

底层代码,选择您想要的单词。我写了这些单词,我想点击链接。单词可能有误,所以我使用Google Translator。只有“范围”值声明如何点击?谢谢。

if (webBrowser1.Document != null)
            {
                IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
                if (document != null)
                {
                    IHTMLSelectionObject currentSelection = document.selection;

                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null)
                    {
                        const String search = "Sayfalar";

                        if (range.findText(search, search.Length, 2))
                        {
                            range.select();

                        }
                    }
                }



            }

2 个答案:

答案 0 :(得分:1)

要提交表单,您只需要知道提交按钮的ID

VB.NET

For Each html As System.Windows.Forms.HtmlElement In  WebBrowser1.Document.GetElementsByTagName("a")
            If html.InnerText = "YOUR TEXT" Then
                html.InvokeMember("click")
            End If
Next

C#

foreach (System.Windows.Forms.HtmlElement html in WebBrowser1.Document.GetElementsByTagName("a")) {
    if (html.InnerText == "YOUR TEXT") {
       html.InvokeMember("click");
    }
}

如果您有任何疑问,请联系。

答案 1 :(得分:0)

我不确定你在这里问什么。你在使用C#要求webbrowser自动化吗?

这是使用mshtml点击的示例:

using mshtml;

var element = webBrowser1.Document.GetElementById("q");
  ((IHTMLElement)element.DomElement).click();

PS。 Alexander Kent使用C#撰写了关于webbrowser自动化的精彩文章 ,你可以找到它HERE