C# 单击具有多个相同类的类的按钮
当在C# 单击具有多个相同类的类的按钮,c#,C#,当在WebBrowser中单击网页上的按钮时,我必须使用以下代码通过元素类执行此操作: HtmlElementCollection classButton = WebBrowser1.Document.All; foreach (HtmlElement element in classButton) { if (element.GetAttribute("className") == "button color") { element.InvokeMember("c
WebBrowser
中单击网页上的按钮时,我必须使用以下代码通过元素类执行此操作:
HtmlElementCollection classButton = WebBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("className") == "button color")
{
element.InvokeMember("click");
}
}
我想单击具有类
按钮颜色的按钮,但它们是同一类的12个按钮,我只想模拟单击第一个按钮。我决定将我的注释移动为答案。由于juansanchez1993只想在12个具有相同类名的按钮中模拟单击第一个按钮,因此简单的解决方案是在为按钮调用InvokeMember
后,在foreach
循环中添加break
。以下是我的意思的一个例子:
HtmlElementCollection classButton = WebBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("className") == "button color")
{
element.InvokeMember("click");
break; //Add the break after the InvokeMember is called to only invoke the 1st button click.
}
}
下面是我在评论中使用System.Linq
调用特定类按钮的意思:
HtmlElementCollection classButton = WebBrowser1.Document.All.Where(e => e.GetAttribute("className") == "button color") as HtmlElementCollection;
classButton[index].InvokeMember("click");
免责声明:我以前从未使用过HtmlElementCollection
,因此如果您对此有疑问,请务必添加一些注释。欢迎使用StackOverflow!为什么有12个按钮具有相同的类名?如果您希望它只调用单击找到的第一个按钮,那么您可以添加一个中断调用InvokeMember
调用后的foreach循环中的code>。第二个答案不起作用,是否给出第二个答案或其他地方?是否给出第二个答案或其他地方?错误错误1'System.Windows.Forms.HtmlElementCollection'no contiene una definificialón de'Where'ni se encontrol ningún método de extension'Where'queacepte un primer argumento de tipo“System.Windows.Forms.HtmlementCollection”(“我们的指南”和“参考资料”)错误2 El nombre“索引”不存在实际使用系统的上下文。Linq;第二个答案不起作用,是否给出第二个答案或其他答案?您是否能够将HtmlElementCollection强制转换为列表
?我100%确信List
可以使用Where
LINQ方法。我不确定HtmlElementCollection
,因为我以前从未使用过它。也许对你有帮助。我认为进展顺利但没有很好地提高他们的代码,遗憾的是我对它了解不多