Check if a class element exists depending the class attribute using Xpath

247 Views Asked by At

I need to get the ratings(stars) of a particular company using Xpath .Below is the block of div element from which I need to decide if its a 5 star , 4 star , 3 star etc depending the class attribute

div class="star-rating star-rating-4 star-rating--medium" div

"star-rating star-rating-4 star-rating--medium" , if this is class with 4 star rating I need to return 4 , 5 if its a 5 star rating and so on .One way I can do this is try to get Xpath of all variants(3,4,5 star rating) and whichever doesn't turn up as null return that .Is there any better way to achieve this ?

1

There are 1 best solutions below

0
supputuri On

Here is the xpath Based on the class of div:

 //div[@class='star-rating star-rating-4 star-rating--medium']/substring(@class,string-length('star-rating star-rating-')+1,1)

OutPut: 4 enter image description here

Based on index of the rating div:

  //div[starts-with(@class,'star-rating star-rating-')][2]/substring(@class,string-length('star-rating star-rating-')+1,1)

This will return the rating of 2nd div with class name that starts with star-rating star-rating- enter image description here