gpt4 book ai didi

C#网络浏览器单击带有类名的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:13 25 4
gpt4 key购买 nike

我需要有关单击带有类名的按钮的帮助。我想点击下面的这个按钮。

<a href="https://www.supremenewyork.com/checkout" class="button checkout">checkout now</a>

我试过了,但按钮没有点击。

HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("button checkout") == "button")
{
element.InvokeMember("click");
}
}

最佳答案

您正在寻找一个名为“button checkout”的属性而不是类。您可能也应该使用 .Contains 这样如果有多个类那么它就不会错过它们:

HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("class").Contains("button"))
{
element.InvokeMember("click");
}
}

如果你想确保你找到的是“button”而不是“redbutton”之类的东西,那么改为:

HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (Regex.IsMatch(element.GetAttribute("class"), @"\bbutton\b"))
{
element.InvokeMember("click");
}
}

您也可以使用 LINQ 进行简化:

webBrowser1.Document.All.Where(
element => element.GetAttribute("class").Contains("button"))/*OR regex in example 2*/
.ToList().ForEach(element => element.InvokeMember("click"))

关于C#网络浏览器单击带有类名的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33439447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com