15

I have HTML which looks basically like the following:

...    
  <a class="btnX btnSelectedBG" href="#"><span>Sign in</span></a>
...

The following xpath in Selenium fails to find an element:

//a[contains(text(), 'Sign in') and contains(@class,'btnX')]

The following xpaths in Selenium succeed, but are not specific enough for me.

//a[contains(text(), 'Sign in')]
//a[contains(@class, 'btnX')]

Why is the xpath failing to find an element, and what can I do to get it to work?

3
  • 1
    //a[contains(text(), 'Sign in')] shouldn't select that element
    – user357812
    Apr 27, 2011 at 19:18
  • Yeah, of the two options that work I would agree that the first should not work -- unless it is fetching the text() of the subelements and joining them, as JavaScript does. However, I was having trouble reconciling that with the fact that it didn't work in conjunction with the other test except when I made up some sort of fuzzy logic that once I used a particular "context" element for the expression, it was unable to evaluate the rest of the expression outside of that context.
    – cgp
    Apr 27, 2011 at 19:39
  • It will work because it is collecting the text of sub-elements as well. This can be a pain and a boon :)
    – Ardesco
    Apr 28, 2011 at 13:52

4 Answers 4

24

Match cases where Sign in is directly child of a or child of another element:

//a[contains(@class,'btnX') and .//text()='Sign in']

I mean

<a class="btnX btnSelectedBG" href="#">Sign in</a>

and

<a class="btnX btnSelectedBG" href="#"><b>Sign in</b></a>

5

//a[contains(@class,'btnX') and span[text()='Sign in']] is not a good idea because you are going to search through the DOM for every anchor and then try and compary it to your search criteria.

You ideally want to key your XPath to the first ascendant element that has an ID and then work your way down the tree.

e.g. if your html is

<div id="foo">   
  <a class="btnX btnSelectedBG" href="#"><span>Sign in</span></a>
</div>

You could use:

//div[@id='foo']/a[contains(@class, 'btnX')][span[.='Sign in']]

Unfortunatly I don't know the rest of the structure of the page so I can't give you anything more concrete than:

//a[contains(@class, 'btnX')][span[.='Sign in']]

but it is really not a very nice xpath.

(My XPath's look slightly different to you because I have used . as a shortcut for text() and a second set of [] as a shortcut for and)

1
  • I was curious about the . notation and when it could be used. Thanks for the example.
    – cgp
    Apr 29, 2011 at 14:39
0

Yaaa for me. I think this is the best answer, but open to other solutions!

//a[contains(@class,'btnX') and span[text()='Sign in']]
2
  • 1
    Of course: //a[contains(@class,'btnX') and contains(.,'Sign in')]. Do not address text nodes in mixed content schemas like XHTML.
    – user357812
    Apr 27, 2011 at 19:48
  • You do realise that that is functionally identical to //a[contains(@class, 'btnX')][span[.='Sign in']] Instead of using an and I have just broken them into two conditions :)
    – Ardesco
    Apr 15, 2013 at 22:19
0

For and operation the below point is important.

“and” is case-sensitive, you should not use the capital “AND”.

Syntax: //tag[XPath Statement-1 and XPath Statement-2]

Here you can also find many other ways to find Xpath in Selenium: https://www.swtestacademy.com/xpath-selenium/

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.