Select Page
Codoid Blog

Useful XPath Expressions

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

For Selenium beginners, the below listed XPath examples are really helpful to write a relative XPath instead of messing up with an absolute one.

1) Select First & Last Node
  • Selecting first hyper link in a page_xpath=/descendant::a[1]
  • Selecting first link using Position method_xpath=/descendant::a[position()=1]
  • Selecting last link_xpath=/descendant::a[last()]
  • Selecting first link which has innerText as ‘English’:xpath=/descendant::a[text()=’English’][1]
  • Selecting first input tag which has ‘class’ attribute_xpath=/descendant::input[@class][1]
2) Selecting a node using multiple condition
  • Selects input tag using ‘type’ and ‘class’ attributes value_xpath=//input[@type=’text’ and @class=’gbqfif’]
  • Selects a button which has name and class attributes: xpath=//button[@name and @class]
3) Getting parent node from child node
  • Gets the input tag’s parent node using ‘..’ : xpath= //input[@name=’q’]/..
4) Selecting parent node from child node
  • Selects the ‘DIV’ which is having ‘INPUT’ tag: xpath=//div[input]


Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us