I am trying to automate downloading files from a website. I have used xpaths to control a website in java in the past, and would like to use them in VBA for this project. Is it possible to use something like getElementByXpath() and if so, what references would I need to include in my project?
3 Answers
It looks like it's possible. My initial reference is to WiseOwlTutorials : https://www.youtube.com/watch?v=sGw6r5GVA5g
Another reference (more specific to XML / using Xpaths) is from AnalystCave: http://analystcave.com/vba-xml-working-xml-files/
W3Schools looks like it has some good information on syntax as well: https://www.w3schools.com/xml/xpath_syntax.asp
You will need to go into Tools > References > Select Microsoft XML, v6.0 (or latest) and then set a DOMDocument which will have a collection of nodes.
Dim XDoc As MSXML2.DOMDocument60
Set List = XDoc.SelectNodes("//FirstChild/ChildNodes[reference number]/innerText")

- 1,148
- 6
- 20
- 43
It's definitely possible, but in my experience I have found it easier to work around XPATH by locating using tag names or class.
It looks like there is plenty of discussion online for using Selenium VBA if that's applicable to your use case.
Private IE As Object
Set IE = CreateObject("InternetExplorer.Application")
URL = "https://www.google.co.uk/"
IE.Visible = False/True
IE.Navigate URL
IE.document.getElementsByClassName("*class name*")
IE.document.getElementsByTagName("*tag name*")

- 101
- 6