natehawkboss
natehawkboss

Reputation: 113

xpath expression to find either one type of element or another

I am trying to get either a parent li or tr from an element with specific text.

As it stands I am just trying different separate xpath expressions, but I am curious if I can just make one that goes something like this:

'//[tr | li]//*[contains(text(), "Some Text")]'

or

'//*[contains(text(), "Some Text")]/[ancestor::tr | ancestor::li]'

I have found that I can just string the statements together like so:

'//li[descendant::*[contains(text(), "Some Text")]] | //tr[descendant::*[contains(text(), "Some Text")]]'

but that isn't ideal either.

Upvotes: 1

Views: 715

Answers (2)

E.Wiest
E.Wiest

Reputation: 5905

Assuming the following XPath is your desired expression :

//li[descendant::*[contains(text(), "Some Text")]] | //tr[descendant::*[contains(text(), "Some Text")]]

You can do :

(//tr|//li)[.//*[contains(text(), "Some Text")]]

Upvotes: 1

kjhughes
kjhughes

Reputation: 111621

Instead of

//[tr | li]

use

//*[self::tr or self::li]

Upvotes: 1

Related Questions