How to use getElementsByClassName on an Internet Explorer object in VB.Net?

Asked

Viewed 726 times

1

I’m trying to use a getElementsByClassName in an Access macro.
You are showing line error:

If ie.Document.GetElementsByClassName("msgErroAdic").Length > 1 Then

Being Dim ie as Object and Set ie = CreateObject("InternetExplorer.Application") Returns the error that cannot locate the method. I found this site: https://msdn.microsoft.com/en-us/library/ms535862%28v=vs.85%29.aspx#methods
There is no Getelementsbyclassname method in the Internetexplorer object. However, on this other site: https://msdn.microsoft.com/en-us/library/hh773165%28v=vs.85%29.aspx there is the Getelementsbyclassname method.

I’d like to know the difference.

I also saw other questions where they indicate creating a function, but also did not work. Public Function GetElementsByClassName(Source As HtmlDocument, ClassName As String) As HtmlElement() Build error: "User-defined type not defined".

  • Try to define ie as variant and then create the object with CreateObject("InternetExplorer.Application"). See if it helps and let me know.

  • Yeah, I forgot that part, it’s already like this.

  • @Pedromvm I tried to define as Variant, but it didn’t work either.

1 answer

0


Menu Referências

In the References menu, you must activate:

  • Microsoft Internet Controls
  • Microsoft HTML Object Library

The code below was the basis for solving the problem.
The secret is that the property GetElementsByClassName is only HTML elements, so it was necessary to define the object as HTMLDocument before using GetElementsByClassName

Public Sub navegar()

Dim ie As SHDocVw.InternetExplorer
Dim html As HTMLDocument
Dim elementos As Object
Dim item As Object

Set ie = New InternetExplorer
ie.Visible = False

ie.Navigate "/"

Do While ie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop

Set html = ie.Document
Set elementos = html.getElementsByClassName("horizontal-manchete")

For Each item In elementos
    Debug.Print Trim(item.innerText) & vbCrLf
Next

MsgBox "Fim"

ie.Quit
Set ie = Nothing

End Sub

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.