0

I have been searching the web for an answer but couldn't find anything useful so far. Question is as simple as the title actually. I have some elements in a web page and I would like my macro to perform actions only when they have/not have a specific class.

To be more precise these items are getting hidden with a hidden class added to them whenever you move within the page and as they still can be found getelementbyid I am kind of out of option to verify if this element is visible (hidden class is removed) or not.

I don't think any code needed? It is just some simple elements toggling hidden class and I am curious if I can check it with vba.

Thanks a lot in advance!

  • If you can get the element by id, then you can use the classname? – Nathan_Sav Feb 11 '20 at 14:54
  • Maybe, just try Autoit with IE.au3 – Xenobiologist Feb 11 '20 at 15:36
  • You can do it with VBA, track `document.getElementsByClassName("your class name").length` at different points of the webpage. I'd recommend stepping into a subprocedure, and just having that set as a watch, then going through the webpage to trigger the element class changes. You can also expand the enumerable in the watch window and see the elements in the `HTMLCollection` but the UI is not kind. – jclasley Feb 11 '20 at 16:00
  • @Xenobiologist I can't use tools which are not approved by my company. – Zeynel ÖZTÜRK Feb 12 '20 at 08:38
  • @Nathan_Sav I can get the element by id. So are you suggesting that I will pick the element with id and then check if that element has some certain class? So I can loop until it gets this class. Sounds good but how do I check what class names an element has? – Zeynel ÖZTÜRK Feb 12 '20 at 08:40

1 Answers1

0

I figured I'd flesh it out for you in an answer, as the comment I left isn't all too informative.

Set IE = CreateObject("InternetExplorer.Application")
Dim URL as String
URL = 'your URL here

Dim HTMLdoc As HTMLDocument

With IE
    .Silent = True
    .Visible = True
    .Navigate URL

    Do Until .readyState = 4
        DoEvents
    Loop

    Set HTMLdoc = .Document
End With

Dim hiddenElements As HTMLElementCollection
Set hiddenElements = HTMLdoc.getElementsByClassName("your class name") 'add this to a watch or inspect your locals
'Alternatively, you can print them all to the immediate window
Dim hiddenElement as Variant
For Each hiddenElement in hiddenElements
    Debug.Print hiddenElement.Value 'or .innerHTML or whatever you want here
Next hiddenElement

This works well if you're stepping through code, but using HTMLDocument and readyState can be cumbersome with realtime updating of the webpage, so MSXML.XMLHTTP60 is preferred..

jclasley
  • 668
  • 5
  • 14
  • Thanks a lot for the answer and your time! As far as I understand, you are suggesting to get the list of hidden elements and loop within them to see which ones are hidden by value or some other identifying feature. Can this feature be id for example? Is there a way to check to this;
    Dim hiddenElements As HTMLElementCollection
    Set hiddenElements = HTMLdoc.getElementsByClassName("your class name") 
    For Each hiddenElement in hiddenElements
        Debug.Print hiddenElement.id `Can I see the id of an element like this?
    Next hiddenElement`
    
    – Zeynel ÖZTÜRK Feb 12 '20 at 08:45
  • I have tested and yes I can do it.. Thanks a lot for the idea! – Zeynel ÖZTÜRK Feb 12 '20 at 09:08