Extracting text from table rows

I am grabbing prices from one of my Vendors web shopping cart
The cart uses tables with some divs inside the table

But I am having trouble extracting text inside the tables from just the td's

I am doing this via Visual Basic in Excel 2007 - VendorURL is a variable and so is Xpath in my code shown below
This is the example page link
Page link is here

I can get the the whole table by doing this


Xpath = "//*[@id='itemPrice1713']/div1/table"
Worksheets(WorkSheet2).Cells(1, 1).Value = Application.Run("XPathOnUrl", VendorURL, Xpath)

The code above fetches the whole table and that data looks like this
QuantityPrice1 – 9$31.8310 – 19$30.2420 – 99$28.65100 +$28.01

Ok, so fetching the whole table works, but now I want to extract data from rows and columns separately

This is the xpath to one of the td's in the table

Xpath = "//*[@id='itemPrice1713']/div1/table/tbody/tr[5]/td[2]"
Worksheets(WorkSheet2).Cells(1, 1).Value = Application.Run("XPathOnUrl", VendorURL, Xpath)

I get nothing but a blank cell from the example above
What am I doing wrong here?
Thanks for any help


This is what the table looks like on the web page example

<table class="bglt" border="0" cellspacing="0" cellpadding="0">
<tbody>

<tr>
<td class="smalltext">
<div class="listheadernosort">Quantity</div>
</td>

<td class="smalltext">
<div class="listheadernosort">Price</div>
</td>
</tr>

<tr>
<td class="texttable" nowrap="">1 – 9</td>
<td class="texttable">$31.83</td>
</tr>

<tr>
<td class="texttable" nowrap="">10 – 19</td>
<td class="texttable">$30.24</td>
</tr>

<tr>
<td class="texttable" nowrap="">20 – 99</td>
<td class="texttable">$28.65</td>
</tr>

<tr>
<td class="texttable" nowrap="">100 +</td>
<td class="texttable">$28.01</td>
</tr>

</tbody>
</table>

@nielsbosma
Can you help with this?
Thanks

Try and remove the "/tbody" part of the xpath? Look at the source of the webpage and you'll see there's no tbody element in this table. (I'm guessing you're getting your XPATH from Chrome developer tools and those don't always work directly with the XPATH engine that SeoTools uses.)

@nielsbosma
You are correct, I am getting my Xpath from chrome developer tools by inspecting

I did not even notice the table was missing the Tbody element

I changed my xpath variable to this and it pulled the pricing information from one of the td's

Xpath = "//*[@id='itemPrice1713']/div[1]/table/tr[5]/td[2]"

Thank you so much for your help and your excellent program

I was able to parse out the table td's
But now, I just realized that the item ID is going to change with every item that I need to get prices for

The 3rd div down id="itemPrice1720"
The 1720 number will change on every item, so my original xpath does not work on any other pages

I tried changing my xpath to the id="cartOptions-content" and then stepping down 3 div's, but I get nothing
Xpath = "//*[@id='cartOptions-content']/div[3]/table/tr[2]/td[2]" ' get just a price from a td

Is there a wildcard function?
@id='itemPrice****

Here's is all the html for one item on one page

<!-- Start Prices and Add to Cart Button -->
<div class="pull-left pdp-full-width borderBottom" id="cartOptions-content">
  <div class="pull-left pdp-full-width" id="pdp-price">
    <div class="price" id="itemPrice1720">
      <div class="sales-price-list-html">
        <table class='bglt' border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class='smalltext'>
              <div class='listheadernosort'>Quantity</div>
            </td>
            <td class='smalltext'>
              <div class='listheadernosort'>Price</div>
            </td>
          </tr>
          <tr>
            <td class="texttable" nowrap="nowrap">1&#160;–&#160;9</td>
            <td class="texttable">$22.99</td>
          </tr>
          <tr>
            <td class="texttable" nowrap="nowrap">10&#160;–&#160;19</td>
            <td class="texttable">$21.71</td>
          </tr>
          <tr>
            <td class="texttable" nowrap="nowrap">20&#160;–&#160;99</td>
            <td class="texttable">$21.07</td>
          </tr>
          <tr>
            <td class="texttable" nowrap="nowrap">100&#160;+</td>
            <td class="texttable">$19.80</td>
          </tr>
        </table>
        <br></br>
      </div>
    </div>
  </div>
</div>

I believe I figured it out
By going up one div and getting the id='pdp-price'
I was missing one more forward slash after the div the first time I tried it

This one works on any page regardless of the itemprice code
Xpath = "//*[@id='pdp-price']/div//table/tr[2]/td[2]"

1 Like

You also could have used the contains() XPATH function: https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/contains