Selenium: Finding either, or .. xpath element

485
February 16, 2017, at 03:15 AM

I want to find element with xpath in Selenium, which contain text, but has two possible cases. Here there are :

.//li/a[contains(., 'blah')]
.//li/a/span[contains(., 'blah')]

How to cover the two cases with single xpath.

Second question if possible I want to get as result pointer to the a-element, not to the span in both cases.

thanks

PS> BTW is there a general way to return as a match parent of the matched element, instead ?

Answer 1

As Josh Crozier asks in the comments, yes, .//li/a[contains(., 'blah')] covers both cases.

The string value of a necessarily will contain "blah" if any of its descendant span element's string values contain "blah".

Second question if possible I want to get as result pointer to the a-element, not to the span in both cases.

.//li/a[contains(., 'blah')] will return such a elements.

Be aware that it will also return such a elements as

 <a>xxxblah</a>
 <a><span>blah</span></a>
 <a><span>bl</span><span>ah</span></a>

PS> BTW is there a general way to return as a match parent of the matched element, instead ?

Well, appending /.. to an XPath will return the parent, but I suspect you'd benefit from learning about the string value of XML elements. See Testing text() nodes vs string values in XPath

Answer 2

You should test the xpath I'm giving you to make sure it doesn't return more matches than you want, but I'd just use: //a[contains.,'blah')]

Answer 3

You can try something like this .//li/*[contains(., 'blah')]

Rent Charter Buses Company
READ ALSO
How to implement correctly web.xml for spring-mvc and spring-security together

How to implement correctly web.xml for spring-mvc and spring-security together

Hello I am trying to develop a spring mvc web application includes spring security and hibernateI searched on Google but I could not figure out how to concate spring mvc configurations and spring security configurations in web

465
How to use old JNDI name standard in JBoss 7.1

How to use old JNDI name standard in JBoss 7.1

My application was running in Jboss 61 where were using JNDI name in old standard i

380
I don&#39;t know how put distance between buttons [duplicate]

I don't know how put distance between buttons [duplicate]

This question already has an answer here:

294