我在互联网上搜索并找到了例子;
“如何在 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>
最佳答案
您发布的代码自然找不到您发布的标签。它正在寻找 input
类型的标签:
webBrowser1.Document.GetElementsByTagName("input")
但正如您所说(并演示):
But my webpage button has different tags.
因此,您需要查找您使用的标签。像这样:
webBrowser1.Document.GetElementsByTagName("a")
这将返回文档中的 anchor 元素。然后,很自然地,您需要找到您要单击的特定对象。这就是这条线正在做的事情:
if (element.GetAttribute(attribute).Equals(attName))
它是否找到目标标签完全取决于这些变量的值,我假设您知道并且可以管理这些值。
关于c# - 如何以编程方式单击按钮 - WebBrowser ( IE ) 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987983/