1

my element is :

<div class="class1 class2">

but in some cases, there is an extra class like this:

<div class="class1 class2 fixClassCustom">

how can I handle that in xpath

i used to do this:

.//div[@class="class1 class2"]

but how can i handle both situations ?

Ania David
  • 1,168
  • 1
  • 15
  • 36

1 Answers1

3

You can use contains() and concat():

.//div[contains(concat(' ',@class,' '), ' class1 ') and 
       contains(concat(' ',@class,' '), ' class2 ')]

Note that concat() helps to avoid false positives - for instance, the expression would not match <div class="class10 class20"> case while the //div[contains(@class, 'class1') and contains(@class, 'class2')] expression that does not use concat() would match the element.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • what's the reason behind concatenating? edit -> Nevermind, nice edit – Rafael Almeida May 08 '16 at 16:05
  • @RafaelAlmeida sure, updated with a sample. Thanks. – alecxe May 08 '16 at 16:06
  • @alecxe it is not working, this is the website http://propertyonline.ae/property/listing/category/list_all/Residential/Rent/10?listing_category=Residential&listing_sub_category=Rent&page_size=10&order_by=&sort_by=&display_type=list&listing_type=&bedroom=&sq_ft=&price=&price= and this is the xpath `.//div[@class='properties-rows']/div[@class='row']/div[contains(concat(' ',@class,' '), ' property ') and contains(concat(' ',@class,' '), ' span9 ')]` it gives empty – Ania David May 08 '16 at 16:12
  • @alecxe put it is not empty in the website, as you can see here http://i.stack.imgur.com/aZam3.png – Ania David May 08 '16 at 16:14
  • @AniaDavid interesting..trying it in the Chrome Console: `$x(".//div[@class='properties-rows']/div[@class='row']/div[contains(concat(' ',@class,' '), ' property ') and contains(concat(' ',@class,' '), ' span9 ')]")` and getting multiple results - both with the `featured` class and without.. – alecxe May 08 '16 at 16:17