XPath locator for 2 different element types ?

  • 2 Replies
  • 1472 Views
XPath locator for 2 different element types ?
« on: April 10, 2016, 16:35:05 pm »
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.
« Last Edit: April 10, 2016, 16:36:43 pm by John »

Re: XPath locator for 2 different element types ?
« Reply #1 on: April 10, 2016, 18:56:44 pm »
Hi,

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

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

hope this helps

Re: XPath locator for 2 different element types ?
« Reply #2 on: April 10, 2016, 19:18:40 pm »
Yeah thats exactly what i needed, thanks !

 

Sitemap 1 2 3 4 5 6 7 8