0

I keep getting this error when trying to implement 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.Document.getElementsByClassName("search-button").InvokeMember("submit")

Any tips? Thanks!

2 Answers2

1

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

Test on VS2008 to VS2017 it working

Dũng IT
  • 2,751
  • 30
  • 29
0

I don't think that HtmlDocument, which you get out of the WebBrowser.Document property has a getElementsByClassName method you can invoke.

I think your options are:

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

  2. If you know the tag name, you can call GetElementByTagName and then iterate through this to find the element of the relevant class and then Invoke Member against 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 All property like has been referenced in another answer, but I think you have to iterate through and check each element using GetAttribute like referenced in point (2)

Community
  • 1
  • 1
nkvu
  • 5,551
  • 2
  • 16
  • 12