xpath help to get buttons under a class where a link contains some href value

674 Views Asked by At

I'm trying to write some automated tests for this site https://www.jigsaw-online.com/basket/viewbasket

I'm trying to write a test to add or remove qty from a specific item added to the basket page.

I'm having trouble writing xpath that will get me the element for a button where the link contains some value in the href.

Take the add qty button for example this will be get me all the buttons on page

//button//i[@class='fa fa-plus']

This will get me all the items in the basket__items class where the link contains the product I am wanting to add qty to

//ul[@class='basket__items']//a[contains(@href,'12')]

I'm just having trouble combining these two pieces of xpath to get me the add qty button for the product I want to add too.

Can someone help me with this?

1

There are 1 best solutions below

0
On

This is one possible way to combine the two XPath expressions (formatted for readability) :

//li[
    contains(@class,'basket__row') 
        and 
    .//a[contains(@href,'12')]
]
//button//i[@class='fa fa-plus']

Explanation :

  • Basically, the XPath starts off with //li[contains(@class,'basket__row')], expression that select individual basket item row.
  • and .//a[contains(@href,'12')] in predicate narrow down the result to specific basket item row that you're interested in.
  • from this point, it is straightforward to incorporate your first XPath //button//i[@class='fa fa-plus'], which will return the button from the selected basket item row