ziaprog
ziaprog

Reputation: 1557

Select elements which has certain descendent using Xpath

I want to Select all the LI elements which contain SPAN with id="liveDeal152_dealPrice" as descendents. How do i do this with xpath?

Here is a sample html

<ul>
    <li id="liveDeal_152">
        <p class="price">
            <em>|
                <span class="WebRupee">₹ </span>
                <span id="liveDeal152_dealPrice">495 </span>
        </p>
    </li>

    <li id="liveDeal_152">
        <p class="price">
            <em>|
                <span class="WebRupee">₹ </span>
                (price hidden)
        </p>
    </li>
</ul>

Upvotes: 0

Views: 895

Answers (2)

Petr Janeček
Petr Janeček

Reputation: 38444

Use this

//li[.//span[@id="liveDeal152_dealPrice"]]

It selects

ALL <li> ELEMENTS
//li[                                    ]
     THAT HAVE A <span> DESCENDANT
     .//span[                           ]
             WITH id ATTRIBUTE EQUAL TO "liveDeal152_dealPrice"
             @id="liveDeal152_dealPrice"

That said, it doesn't seem like a very wise element selection, mostly due to the dynamically looking id. If you're going to use it once, it's probably ok, but if you're using it, say, for testing and will reuse it many times, it might cause trouble. Are you sure this won't change when you change your website and/or database?


As a side note:

  • ul stands for "unordered list"
  • ol stands for "ordered list"
  • li stands for "list item"

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

//li[.//span[@id = 'liveDeal152_dealPrice']] should do. Or more verbose but closer to your textual description //li[descendant::span[@id = 'liveDeal152_dealPrice']].

Upvotes: 1

Related Questions