gpt4 book ai didi

c# - 如何通过 GetElementByClass 选择一个类并以编程方式单击它

转载 作者:太空狗 更新时间:2023-10-29 14:41:59 25 4
gpt4 key购买 nike

我一直在尝试使用此代码在 html/ajax 中按类读取元素,因为我知道 GetElementByClass 不是 webBrowser.Document 中的一个选项。我似乎无法获得返回值然后调用该成员。有解决办法吗?

引用资料: Getting HTMLElements by Class Name

例子:

<span class="example">(<a href="http://www.test.com/folder/remote/api?=test" onclick=" return do_ajax('popup_fodder', 'remote/api?=test', 1, 1, 0, 0); return false; " class="example">test</a>)</span>

示例代码:

   HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
//If curElement.GetAttribute("class").ToString = "example" It doesn't work.
// This should be the work around.
if (curElement.OuterHtml.Contains("example"))
{
MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire.
// InvokeMember(test) after class is found.
}
}

最佳答案

我承认这不是很直观,但您需要使用 GetAttribute("className") 而不是 GetAttribute("class")

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
if (curElement.GetAttribute("className").ToString() == "example")
{
MessageBox.Show(curElement.GetAttribute("InnerText")); // Do something you want
}
}

关于c# - 如何通过 GetElementByClass 选择一个类并以编程方式单击它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3436116/

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