试图在 VB.NET 中使用 getElementsByClass ..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22466096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:08:07  来源:igfitidea点击:

Trying to use getElementsByClass in VB.NET..?

vb.netbrowserinvokegetelementsbyclassname

提问by Jordan ChillMcgee Ludgate

I keep getting this error when trying to implement getElementsByClassName..

尝试实现 getElementsByClassName 时,我不断收到此错误。

Error   1   'getElementsByClassName' is not a member of 'System.Windows.Forms.HtmlDocument'.

I'm trying to press a button on webbrowser1 by doing:

我试图通过执行以下操作来按下 webbrowser1 上的按钮:

WebBrowser1.Document.getElementsByClassName("search-button").InvokeMember("submit")

Any tips? Thanks!

有小费吗?谢谢!

回答by nkvu

I don't think that HtmlDocument, which you get out of the WebBrowser.Documentproperty has a getElementsByClassNamemethod you can invoke.

我不认为您从属性中获取的HtmlDocumentWebBrowser.Document具有getElementsByClassName您可以调用的方法。

I think your options are:

我认为你的选择是:

  1. If you know the Id, you can call GetElementByIdand then InvokeMemberagainst that, for example: WebBrowser1.Document.GetElementById("searchButtonId").InvokeMember("submit")

  2. If you know the tag name, you can call GetElementByTagNameand then iterate through this to find the element of the relevant class and then Invoke Memberagainst that.

    If you look at the code sample, you'll see a elem.GetAttribute("content")call - you'll probably want to do something similar but with elem.GetAttribute("class")and inspect what gets returned to make sure it's the class name you want

  3. Use the Allproperty like has been referenced in another answer, but I think you have to iterate through and check each element using GetAttributelike referenced in point (2)

  1. 如果您知道 Id,您可以调用GetElementById,然后InvokeMember针对该 ID,例如:WebBrowser1.Document.GetElementById("searchButtonId").InvokeMember("submit")

  2. 如果您知道标记名称,则可以调用GetElementByTagName,然后遍历它以找到相关类的元素,然后再Invoke Member针对该元素进行迭代。

    如果您查看代码示例,您会看到一个elem.GetAttribute("content")调用 - 您可能想做类似的事情,但elem.GetAttribute("class")检查返回的内容以确保它是您想要的类名

  3. 使用All属性 like 已在另一个答案中引用,但我认为您必须迭代并使用GetAttribute(2)点中引用的类似元素检查每个元素

回答by IT Vlogs

This is my solution and can use:

这是我的解决方案,可以使用:

Dim theElementCollection As HtmlElementCollection = Nothing
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
    If InStr(curElement.GetAttribute("classname").ToString, "search-button") Then
        curElement.InvokeMember("Click")
        Exit For
    End If
Next

at GetAttribute("classname")use for class and GetAttribute("id")use for id

GetAttribute("classname")使用类和GetAttribute("id")使用的ID

Test on VS2008 to VS2017 it working

在 VS2008 到 VS2017 上测试它工作