Rich Stevens
Rich Stevens

Reputation: 609

Xpath, use two queries to return two pieces of information instead of both together

I am trying to write two xpath queries to get 2 bits of info separately from 2 divs. Problem is, I only seem to get them both together.

I have tried variations of:

//a[@data-role='sku']/img/@title

This returns both "orange" and "pink"

But what I want is 2 separate queries, like:

//a[@data-role='sku']/img/@title[1] - orange

//a[@data-role='sku']/img/@title[2] - pink

Code below:

<ul id="j-sku-list-1" class="sku-attr-list util-clearfix" data-sku-prop-id="14" data-isselect="true">
                                                                                                                             <li class="item-sku-image"><a data-role="sku" data-sku-id="350852" id="sku-1-350852" title="orange" href="javascript:;" class=""><img src="https://ae01.alicdn.com/kf/HTB1GIlhkL6H8KJjy0Fjq6yXepXa3/Ahagaga-2018-Spring-Summer-Rompers-Woman-Jumpsuits-Fashion-Floral-Print-Loose-Sexy-Women-Playsuits-Regular-Casual.jpg_50x50.jpg" title="orange" bigpic="https://ae01.alicdn.com/kf/HTB1GIlhkL6H8KJjy0Fjq6yXepXa3/Ahagaga-2018-Spring-Summer-Rompers-Woman-Jumpsuits-Fashion-Floral-Print-Loose-Sexy-Women-Playsuits-Regular-Casual.jpg_640x640.jpg"></a></li>
                                                                                                                                                             <li class="item-sku-image active"><a data-role="sku" data-sku-id="1052" id="sku-1-1052" title="pink" href="javascript:;" class=""><img src="https://ae01.alicdn.com/kf/HTB1D916jkfb_uJjSsrbq6z6bVXaT/Ahagaga-2018-Spring-Summer-Rompers-Woman-Jumpsuits-Fashion-Floral-Print-Loose-Sexy-Women-Playsuits-Regular-Casual.jpg_50x50.jpg" title="pink" bigpic="https://ae01.alicdn.com/kf/HTB1D916jkfb_uJjSsrbq6z6bVXaT/Ahagaga-2018-Spring-Summer-Rompers-Woman-Jumpsuits-Fashion-Floral-Print-Loose-Sexy-Women-Playsuits-Regular-Casual.jpg_640x640.jpg"></a></li>
                                                                                    </ul>

Upvotes: 0

Views: 31

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

You haven't shown us the source document, so we're guessing slightly, but I suspect it should be

(//a[@data-role='sku']/img)[1]/@title
(//a[@data-role='sku']/img)[2]/@title

Remember that subscripts bind more tightly than "/", so a/b/c[1] means select every a/b/c where the c is the first child of the corresponding b; if you want the first a/b/c overall, you need (a/b/c)[1].

This means it never makes sense to put [1] (or any other subscript) immediate after @title, because there cannot be more than one @title selected.

Upvotes: 0

Vitaliy Moskalyuk
Vitaliy Moskalyuk

Reputation: 2583

Index elements, which contains images, not titles:

//a[@data-role='sku'][1]/img/@title

//a[@data-role='sku'][2]/img/@title

Or titles, but out of all range of titles:

(//a[@data-role='sku']/img/@title)[1]
(//a[@data-role='sku']/img/@title)[2]

Your query //a[@data-role='sku']/img/@title[2] won't work, as it searches images that has second title, but all your images have only 1 title, I suppose)

Upvotes: 1

Related Questions