TestProject Forum
Powered by leading experts in the test automation community

XPath locator for 2 different element types?

I’m searching for an element with a class ‘dropdown-toggle’ which should toggle down a dropdown menu.
But on some pages the dropdown-toggle is a button element, whereas other pages it is a link (anchor).

How can i write a generic XPath locator to find both (either or) button/link in order to automate it ?

For example: http://alloyui.com/examples/dropdown/
and: http://alloyui.com/examples/dropdown/real-world/

I want to catch both cases in a single XPath.


You can do so with the ‘|’ pipe/or operator.


WebElement dropdownToggle = driver.findElement(By.xpath("//button[contains(@class, 'dropdown-toggle')] | //a[contains(@class, 'dropdown-toggle')]"));

hope this helps

Yeah thats exactly what i needed, thanks !