繁体   English   中英

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

[英]How to Click A Button Programmatically - Button in WebBrowser ( IE )

我在互联网上搜索了一些例子;

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

这是在谷歌工作的代码;

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>

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

webBrowser1.Document.GetElementsByTagName("input")

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

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

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

webBrowser1.Document.GetElementsByTagName("a")

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM