0

How to add OR logic within an xpath locator?

Is it possible to add OR logic within an xpath locator, so they my test scripts are using one or the other?

For example I have constructed the following locator:

//div[contains(@id,'va:results')]//h1 | //div[contains(@id,'va:results')]//h3

The test scripts seem to throw the odd exception:

Exception: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@id,'va:results')]//h1 | //div[contains(@id,'va:results')]//h3"}

But the tests are not failing; as I'm assuming one of the above selectors are being used.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
SamP
  • 417
  • 5
  • 24
  • 2
    Possible duplicate of [Using an OR condition in Xpath to identify the same element](https://stackoverflow.com/questions/34523638/using-an-or-condition-in-xpath-to-identify-the-same-element). You can use `|`, should return existing one (h1 or h3) or both (h1 and h3) – Sers Mar 01 '19 at 10:46

1 Answers1

0

To add OR logic within an xpath expression so your test script can use one or the other you can use the following solution:

//div[contains(@id,'va:results')]//[@h1 or @h3]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352