Reputation: 5215
I am trying to get the category of a product page from amazon.
For example I am trying to get Schaumstoffmatratzen
from the following page.
I am using the following libraries:
I tried the following:
Dim categorieAmzn
Set Docx = Ie.document
categorieAmzn = Docx.getElementsByClassName("cat-name").innerText
However, I get nothing back. Any suggestions what I am doing wrong?
I appreciate your replies!
Upvotes: 0
Views: 3427
Reputation: 4514
Assuming there is more code that navigates to the webpage, the only issue with your code is that you are trying to obtain the innerText
from an element collection, not a single element.
getElementsbyClassName()
always obtains an element collection. In order to get a single element from the collection you will need to specify an index number but it's worth noting that index numbers start at 0 not 1. So for example, if the element you want to get is the first element with the class name of "cat-name" you should use:
categorieAmzn = Docx.getElementsByClassName("cat-name")(0).innerText
The best way to tell if you are getting a collection or a single element is by looking for an 's' in the function, if there is an 's' you will be getting a collection, if there isn't an 's' you are getting a single element.
Single HTML element function:
getElementbyId
HTML Element collection functions:
getElementsbyClassName
getElementsbyTagName
getElementsbyName
Upvotes: 1