Mark_Anderson
Mark_Anderson

Reputation: 1324

getelementsbyclassname Excel vba - error on repeated calls

Morning,

I am having trouble with a webscrape from Excel, whereby getelementsbyclassname is failing to act on some objects, throwing up the "Object doesn't support this property or method" error.

The problem appears when the object I am feeding into getelementsbyclassname is itself the result of a getelementsbyclassname method. I am not sure why, particularly as I can get the class name when acting on a larger object...

Here is a code extract

''''Boring Variables Declaration I've cut out''''

'Initialise IE
Dim IEApp As New InternetExplorer 
Set IEApp = New InternetExplorer 
IEApp.Visible = True 'JB

'Open page and wait for page to load
  IEApp.navigate ("http://www.anicewebsite.com")
  Do Until IEApp.readyState = READYSTATE_COMPLETE And IEApp.Busy = False
    DoEvents
  Loop

Set HTMLdoc = IEApp.document
Set RefLocation = Sheets("INFO_DUMP").Range("LocationRefCell")
Set trElements = HTMLdoc.getElementsByClassName("basic-details")
For Each trElement In trElements
        'Select the LHS box and extract info
        Set tdElement = trElement.getElementsByClassName("tieredToggle")
        'write start/end locations
        '''''THIS NEXT LINE THROWS AN ERROR'''''
         Data_str = tdElement.getElementsByClassName("title").innerText
        '''''AS DOES'''''
        MyObject=tdElement.getElementsByClassName("title")
         RefLocation.Offset(1, 2).Value = Data_str 
Next 'close tr Loop         

However, I can get the 'title' object via

For Each trElement In trElements
        Set MyObject=trElement.getElementsByClassName("title")
Next 'close tr Loop

so the error is, presumably, something about tdElement (a DispHTMLElement Collection), which I tried to attach an image of but I lack the reputation (see link at end of post)...

Many thanks for any help.

PS. the webpage is structured, roughly, with a 2-column table whose rows I isolate with "basic-details". The first column is the "tiered toggle" and then the items I want are inner text in eg. "title". I need to use tieredtoggle as objects in each column have repeated class names

https://i.sstatic.net/1tyb6.png

Upvotes: 2

Views: 786

Answers (1)

Vignesh Shankar
Vignesh Shankar

Reputation: 44

You can use this to get the innertext.

Data_str = tdElement.getElementsByClassName("title")(0).innerText

Instead of ("title")(0) you can enter the index value where the element is present.

Upvotes: 1

Related Questions