Hsaka
Hsaka

Reputation: 11

Conditional Xpath? Is there any way to take the first Xpath if it is present and if not take the other Xpath

So I have a situation where there are two Xpaths. Xpath1 and Xpath2. I have to take Xpath1 if it is there. And if not I have to take Xpath2.

I tried using Xpath1 | Xpath2 .

Issue with this is, if both are present it's Taking Xpath2.

I don't need the Xpath2 if Xpath1 is already there.,

Upvotes: 1

Views: 59

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

If the result is a single node, you can do (path1 | path2)[1].

If the result is a node-set of arbitrary size, then it's more difficult, especially in XPath 1.0 (which is what you're using with Selenium). You might have to resort to something like (path1 | self::node()[not(path1)]/path2).

Upvotes: 1

Related Questions