如何以编程方式单击按钮 - WebBrowser(IE)中的按钮

时间:2012-01-24 13:53:53

标签: c# html

我搜索了互联网并找到了示例;

“如何在C#中单击webBrowser(Internet Explorer)中的按钮?”

这是代码working on google;

JS:

    void ClickButton(string attribute, string attName)
    {
        HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");
        foreach (HtmlElement element in col)
        {
            if (element.GetAttribute(attribute).Equals(attName))
            {
                element.InvokeMember("click");   // Invoke the "Click" member of the button
            }
        }
    }

但我的网页按钮有不同的标签。所以程序无法检测到是否点击它。

我的主要问题是; 如何以编程方式点击按钮?

HTML:

<a class="orderContinue" href="Addresses" title="Sipar Ver">Sipar Devam</a>

2 个答案:

答案 0 :(得分:8)

当然,您发布的代码无法找到您发布的代码。它正在寻找input类型的标签:

webBrowser1.Document.GetElementsByTagName("input")

但正如你所说(并证明):

  

但我的网页按钮有不同的标签。

因此,您需要查找您使用的标记。像这样:

webBrowser1.Document.GetElementsByTagName("a")

这将返回文档中的锚元素。然后,您自然需要找到要单击的特定部分。这就是这条线正在做的事情:

if (element.GetAttribute(attribute).Equals(attName))

是否找到目标标签完全取决于这些变量的值,我假设您知道并且可以管理这些变量。

答案 1 :(得分:-1)

使用jQuery,您可以在该标记上放置一个click事件 您可能希望将此代码放在页面底部

<script>
$(document).ready(function() { 
    $('.orderContinue').click(function() {
       alert('Sipar Ver has been clicked');
       // More jQuery code...
    });
});
</script>
相关问题