0

Am trying to scrape the title and price from mcmaster.com to auto populate an internal purchase rec form. (URL with item part number is hard coded below for testing.) Got the title fine, but can't get the price. I don't think my code is looking at the correct place because the element is not found. I modeled my code from here.

Sub get_title_header()
Dim wb As Object
Dim doc As Object
Dim sURL As String

Set wb = CreateObject("internetExplorer.Application")
sURL = "https://www.mcmaster.com/#95907A480"

wb.navigate sURL
wb.Visible = True

While wb.Busy Or wb.readyState <> READYSTATE_COMPLETE
    DoEvents
Wend

'Wait a bit for everything to catchup...(network?)
Application.Wait (Now + TimeValue("0:00:02"))

'HTML document
Set doc = wb.document

Dim MyString As String
MyString = doc.Title

Sheet2.Cells(1, 2) = Trim(Replace(MyString, "McMaster-Carr -", "", , , vbTextCompare))

Dim el As Object
For Each el In doc.getElementsByName("Prce")
    Sheet2.Cells(2, 2).Value = el.innerText
Next el

wb.Quit

End Sub

Is this iframe nested? Can anyone explain why my code can't see the iframe data and help me get the item price? Thanks in advance!

Community
  • 1
  • 1

1 Answers1

1

The element you try to scrape in your link is nested as follows:

enter image description here

The first mistake you do is that you're trying to get this object by name, and not by id:

doc.getElementsByName("Prce")

There's no object in your HTML document having name="Prce", so I'd expect the For cycle not even to start.

What you might want to do to get your price, instead, is:

Sheet2.Cells(2, 2).Value = doc.getElementById("Prce").getElementsByClassName("PrceTxt")(0).innerText

where:

  • doc.getElementById("Prce") gets you the element <div id="Prce">...</div>
  • .getElementsByClassName("PrceTxt")(0) gets you the first (and only) element inside the above div, i.e. <div class="PrceTxt">...</div>
  • .innerText gets you the text of this element
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • That worked great! Thanks for the explanation and the amazing quick response! StackOverflow is by far my favorite go-to for code questions. – Brother.Ed Nov 01 '17 at 21:28
  • @Brother.Ed glad it worked, please accept my answer so the thread gets closed. – Matteo NNZ Nov 01 '17 at 21:40