【问题标题】:Clicking on the link with the webbrowser c#单击带有 webbrowser c# 的链接
【发布时间】:2011-12-25 16:35:19
【问题描述】:

底层代码,选择你想要的词。我写了这些话我想点击链接。单词可能是错误的,所以我使用谷歌翻译。只有“范围”值声明如何点击?谢谢。

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

                        }
                    }
                }



            }

【问题讨论】:

    标签: c# webbrowser-control


    【解决方案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");
        }
    }
    

    如有任何疑问,请联系。

    【讨论】:

    • 谢谢,但我不想要班级 ID 或提交 ID。我需要话。样本; Meherzad 字如果我看到自动我想点击这个链接。但我不想点击所有链接类或 id=user。并且不是按钮。 @Meherzad
    • 再次感谢,但现在给出错误。代码:webBrowser1.Document.All.GetElementsByName("TextToSearch"); (0).InvokeMember("点击");错误:“int”不包含“InvokeMember”的定义,并且找不到接受“int”类型的第一个参数的扩展方法“InvokeMember”(您是否缺少 using 指令或程序集引用?)再次感谢!
    • 是吗?并且不起作用,因为 (0).InvokeMember("click");
    • if (webBrowser1.Document != null) { webBrowser1.Document.All.GetElementsByName("Sayfalar"); [0].InvokeMember("点击"); } 现在代码是这样的,但又不起作用..
    • 它的 vb.net 代码。您可以使用在线转换器developerfusion.com/tools/convert/vb-to-csharp
    【解决方案2】:

    我不确定你在这里问什么。您是否要求使用 C# 实现网络浏览器自动化?

    这里是使用 mshtml 点击的示例:

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

    ps。 Alexander Kent 写了一篇关于使用 C# 实现网络浏览器自动化的精彩文章 ,你可以找到HERE

    【讨论】:

    • 感谢您的回复,点击我想要链接的词。这就是你可以选择并点击的单词?我对上面选择代码的过程做了一些事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2013-06-15
    • 2012-07-23
    • 1970-01-01
    • 2022-11-14
    • 2017-02-21
    相关资源
    最近更新 更多