0

using excel vba i need to scrape the date (2019-01-16). this code works great:

Set wyjatek = ie.Document.getElementsByClassName("redesignTravelHistory tank-thlist__date 2019-01-16")(0)

but the problem is, that in every html I try to scrape, this date can be different. Do you know how to solve it?

part of html:

<div class="redesignTravelHistory tank-thlist__date 2019-01-16">16/01/2019</div>
1
  • 1
    1. use getElementsByTagName("div") to get a collection of all divs. 2. Loop throught that collection and use className property to read the class name. 3. Use string functions or LIKE to match a pattern in the class name. Commented Jan 23, 2019 at 20:51

2 Answers 2

2

You can use a css attribute = value selector with ^ operator (value starts with substring after the =)

ie.document.querySelector("div[class^='redesignTravelHistory tank-thlist__date']")

If there can be more than one element matched with this then use querySelectorAll and index in to the nodeList to get the right element e.g. index 1

ie.document.querySelectorAll("div[class^='redesignTravelHistory tank-thlist__date']").item(1)
Sign up to request clarification or add additional context in comments.

Comments

1

You are using the first element that contains 3 classes

  • redesignTravelHistory
  • tank-thlist__date
  • 2019-01-16

I'm guessing that the date class "2019-01-16" is irrelevant and can be removed.

 Set wyjatek = ie.Document.getElementsByClassName("redesignTravelHistory tank-thlist__date")(0)

1 Comment

Heck...I didn't realize that you could query multiple class name till I read your post. Thanks for accepting my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.