1

I am working on modifying a fairly extensive VBA Web scraper and looking for some advice on how to get elements by id when there is no actual id specified.

<ul class="rtUL">
    <li class="rtLI"><div class="rtMid">
    <span class="rtSp"></span><span class="rtPlus"></span>
    <a class="rtIn" href="/mhpviewer.aspx?FID=CSTAT">Claim Status</a>
    </div></li><li class="rtLI"><div class="rtMid">
    <span class="rtSp"></span><span class="rtPlus"></span>
    <a class="rtIn" href="/mhpviewer.aspx?FID=EVER">Eligibility Verification</a>

The element I need to get is the <li> containing "Eligibility Verification". Is there a way to get children elements or just inner HTML?

Smandoli
  • 6,919
  • 3
  • 49
  • 83
justkrys
  • 300
  • 3
  • 13
  • Does [this thread](http://stackoverflow.com/questions/11805389/getting-data-from-html-source-in-vba-excel) help? Or maybe [this one](http://stackoverflow.com/questions/12344559/how-to-use-getelementbyid-and-copy-inner-html-vba)? – BruceWayne Jan 20 '16 at 17:46
  • I am dealing with modifying code for hundreds of Web pages and am trying to learn all of the various ways to get at those elements because it may be easier to create variables, ie, they will all require the same type of input so rather than looking for specific id or inner HTML text, I'm trying to find a way to look at say each li in an unordered list, so I can minimize the code. It may not be possible, but someone may have some tips. – justkrys Jan 20 '16 at 18:15

2 Answers2

2

I am assuming you are using InternetExplorer.Application (version 8 and above), have managed to retrieve the page and the format is as per your question.

Set Data = IE.Document.querySelectorAll("li>div>a") 
'looks for tag `a` inside tag `div` inside tag `li`

For Each oA In Data
    If oA.InnerHTML = "Eligibility Verification" Then
        Set oLI = oA.ParentNode.ParentNode
        Debug.Print oLI.InnerHTML
    End If
Next oA
Smandoli
  • 6,919
  • 3
  • 49
  • 83
Jules
  • 1,423
  • 13
  • 22
  • Whenever I try to do anything with this other than Debug.Print, it crashes my Web browser, IEv11. The nested elements tip was a great one though and I think will lead me in the direction I was hoping for. Thankyou! – justkrys Jan 21 '16 at 22:16
1

CSS selector:

You can use a CSS selector of .rtIn[href=/mhpviewer.aspx?FID=EVER]. This says element(s) with rtin class with attribute href having value of '=/mhpviewer.aspx?FID=EVER'


CSS query:

CSS query


VBA:

You can apply for the first matching element with

Debug.Print ie.document.querySelector(".rtIn[href=/mhpviewer.aspx?FID=EVER]").innerHTML
QHarr
  • 83,427
  • 12
  • 54
  • 101