4

I want to get the Xpath selector for classname(.class) . So basically, i want to know how I can select [attr~=value]

So if i have an element

<div class="class1 class2 class3"></div>
<div class="class1"></div>

I want to select .class1, it should return both the divs. [@class='class1'] doesn't work since it won't select the first div.

5
  • //div[contains(@class,'class1')] stackoverflow.com/questions/103325/… Commented Feb 11, 2012 at 14:59
  • 1
    possible duplicate of How can I find an element by CSS class with XPath? Commented Feb 11, 2012 at 14:59
  • @mazzucci: That would match div.class123 Commented Feb 11, 2012 at 15:00
  • Yes, that is true. So he would need something more advanced. Commented Feb 11, 2012 at 15:04
  • Contains will match class123 and fasfasjfhkkdsjfjkclass1 as well Commented Feb 11, 2012 at 15:07

1 Answer 1

6

That trick should only find "class1":

//div[contains(concat(' ',normalize-space(@class),' '),' class1 ')]
Sign up to request clarification or add additional context in comments.

2 Comments

Well I know this one. I was looking for a more elegant solution, but I guess there isn't any
This is XPath, we don't do "elegant" :-) Seriously, this is the best technique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.