0

Say I have multiple price quotes from multiple retailers, how will I retrieve the 5th value from a particular retailer - say Target or Walmart ? I can get to the 5th entry using the matching image logo bit how do I retrieve the value ?

Adding Html Code to make things more clear .I need to retrieve the ratePrice value (198)

<div id="rate-297" class="rateResult standardResult" vendor="15">
    <div class="rateDetails">
        <h4>Standard Goods
            <br>
            <img src="http://walmart.com/walmart/ZEUSSTAR999.jpg">
        </h4>
        <p>
            <span class="vendorPart-380">
                <img alt="Walmart" src="/cb2048547924/icons/15.gif">
                <br>
                <strong>
                <br>
                    MNC
            </span>
        </p> 
    </div>
    <div class="ratePrice">
        <h3>
            $198
            <sup>49</sup>
        </h3>
        <p>
            <strong>$754.49</strong>
            <br>
        </p>
        <a class="button-select" href="https://www.walmart.com/us/order/95134/2013-05-14-10-00/95134/2013-05-17-10-00/297"> </a>
    </div>
</div>
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Zuckerberg
  • 205
  • 6
  • 15

1 Answers1

0

If you could provide some HTML it would help. Speaking generally from what you're asking you'd get a locator to the price div or whatever HTML element and then get its text using something like: _driver.FindElement(locator_of_element).Text

The trick is understanding the HTML in order to target the 5th element. So if you can find the row that has the 5th entry then it's simply a matter or then finding the price div in that row and getting the text of it.

EDIT based on more info provided by OP in comments

Using the HTML you provided (which isn't well formed by the way, missing closing strong tag, a tag, etc.). I'd say do the following:

_driver.FindElement(By.XPath("//div[@class='ratePrice'][5]/h3")).Text

ragamufin
  • 4,113
  • 30
  • 32
  • Standard Goods

    Walmart

    MNC

    $198 49

    $754.49

    I need to retrieve the ratePrice value.
    – Zuckerberg May 08 '13 at 02:59
  • I updated the answer based on the HTML provided, let me know if that works for you – ragamufin May 08 '13 at 03:24
  • Tried both options below : String word= driver.findElement(By.xpath("//div[contains(@class,'ratePrice'][3]/h3")).getText(); String word= driver.findElement(By.xpath("//(div[contains(@class,'ratePrice'])[3]/h3")).getText(); Error : org.openqa.selenium.InvalidSelectorException: The given selector //div[contains(@class,'ratePrice'][3]/h3 is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //div[contains(@class,'ratePrice'][3]/h3 because of the following error: – Zuckerberg May 08 '13 at 03:38
  • 1) What about if you take out the `contains` and simply do `@class='ratePrice'` 2) It may be the fact that the markup is not properly formatted that's the problem. Maybe it can't find anything after that open `strong` tag that is never closed. Try to do it piece by piece, starting with just `//h3` first and then maybe `//div[@class='ratePrice']` and let me know what happens. – ragamufin May 08 '13 at 03:54
  • Thanks, it worked. One more question - What can I do if I dont know the exact rownumber. ex: To find the price of the 4th cheapest Nike shoes from the list of all shoes sorted in price order. Please note the rownumber can vary based on my search criteria. I can identify the Nike shoes by using the logo - xpath="(//img[@alt=\"Nike\"])[4]" But how to get to the price in the same row ? – Zuckerberg May 08 '13 at 04:45
  • I'd have to see the HTML to be sure but what about something like `//img[@alt=\"Nike\"][4]//div[@class='ratePrice']//h3`? – ragamufin May 08 '13 at 05:07
  • No Luck : String temp= driver.findElement(By.xpath("(//img[@alt=\"Thrifty\"])[4]//div[@class=\"ratePrice\"]//h3")).getText(); produced the same error : org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"(//img[@alt=\"Nike\"])[4]//div[@class=\"ratePrice\"]//h3"} Command duration or timeout: 38 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html – Zuckerberg May 08 '13 at 05:36
  • You have parenthesis around only part of the path and it should be around the whole thing such as : `String temp= driver.findElement(By.xpath("//img[@alt=\"Thrifty\"][4]//div[@class=\"ratePric‌​e\"]//h3")).getText();` – ragamufin May 08 '13 at 05:45
  • No luck Still , Something wrong still with the syntax I guess . It would be great if you could execute the above code and let me know if I am missing anything here . – Zuckerberg May 08 '13 at 17:39
  • Don't mind executing the code at all. I'd need a copy of the full html of the page if you can provide it. I'm using C# so the syntax is a little different. For me the above line would be: `string temp = _driver.FindElement(By.XPath("//img[@alt='Thrifty'][4]//div[@class='ratePrice‌']//h3")).Text`. I also noticed that when I copy the line of code from here the 'e' in ratePrice becomes a special character, not sure why, maybe you could try to retype the line and try. What error are you getting now by the way? And have you tried with parts of the locator instead of the full thing? – ragamufin May 09 '13 at 00:43
  • Please let me know an id where I can send the code , the character size limit is not allowing me to paste it here . thanks. – Zuckerberg May 09 '13 at 02:32
  • sure, can you join the room http://chat.stackoverflow.com/rooms/29642/selenium and you can send it to me there – ragamufin May 09 '13 at 07:23