Selenium WebDriver Java Framework Course Limited Time Offer for $50

Selenium WebDriver Java Framework Course Limited Time Offer for $50


Selenium IDE Xpath Tutorials

Selenium Xpath Tutorials

Identifying xpath for element with examples to use in selenium

QA Engineers need to provide any element locator(like id, name, css path, xpath etc.) in target column of selenium window to locate that specific element to perform some action on it .If you have worked with selenium IDE then you knows that sometimes elements does not contains id or name. Locating element by xpath is the another way of locating element and you can use it as a alternative of id or name of element.

Xpath in XML document shows the direction of element location through nodes and attributes. Let we try to understand how to identify Xpath of element with examples.


Above given image is taken from There are three fields 1. Input text box 2. select drop down and 3. input button. And bellow of those fields there is expansion of relative XML nodes through firebug. As you see in image, you can use "id=searchInput" or "name=search" to identify input text box to type something in to it as bellow given example.

Command Target Value
Type id=searchInput Selenium Automation


Command Target Value
Type name=search Selenium Automation


Xpath Element Identification

Locating element using Xpath with Examples for input text box

  Xpath Techniques Examples Explanations
1 Identifying Xpath using full path of XML xpath=//body/div[3]/form/fieldset/input[2] //body is the main root node, /div[3] describes the 3rd div child node of parent node body, /form describes the child node form of parent node div[3], /fieldset describes the child node fieldset of parent node form, /input[2] describes the 2nd input child node of parent node fieldset.
2 Writting Xpath using last() xpath=//body/div[3]/form/fieldset/input[last()-2] /input[last()-2] describes the 3rd upper input node(input[2]) from last input node.
3 Writting Xpath using last() xpath=//body/div[3]/form/fieldset/*[last()-3] /*[last()-3] describes the 4th upper node(input[2]) from last node.
4 Xpath locator using @ and attribute xpath=//body/div[3]/form/fieldset/input[@type='search'] /input[@type='search'] describes the input node having attribute type='search'
5 Xpath expression using @ and attribute xpath=//body/div[3]/form/fieldset/input[@accesskey='F'] /input[@accesskey='F'] describes the input node having attribute @accesskey='F'
6 Xpath syntax using @ and attribute xpath=//input[@accesskey='F'] //input[@accesskey='F'] describes the input node having attribute @accesskey='F'
7 Xpath example using @ and attribute xpath=//input[@type='search'] /input[@type='search'] describes the input node having attribute type='search'
8 XML Xpath using /descendant:: keyword xpath=//div[@class='search-container']/descendant::input[@accesskey='F'] In this case , only starting node div with attribute class='search-container' and final node input with accesskey='F' attribute. So not need to describe in between nodes.
9 Xpath query example using contains keyword xpath=//input[contains(@id, "searchInput")] used contains keyword to identify id attribute with text "searchInput"
10 Xpath using and with attributes xpath=//input[contains(@id, "searchInput") and contains(@accesskey,"F")] two attributes in input node
11 XML xpath value value using position() xpath=//div[@class='search-container']/descendant::input[position()=2] This xpath will select input node which is on number 2 position and it is for input text box as shown in image.
12 Using starts-with keyword xpath=//input[starts-with(@type, "s")] ind input node with attribute is 'type' and its value is starting with 's' (here it will get type = 'search').
  Using OR (|) condition with xpath xpath=//input[@accesskey='F'] | //input[@id='searchInput']

xpath=//input[@accesskey='F' or @id='searchInput']

it will find input text box with accesskey='F' or @id='searchInput'. If any one found then it will locate it. Very useful when elements appears alternatively.
13 Using wildcard * with to finding element xpath xpath=//*[@accesskey='F'] This finds any element that has accessKey attribute="F"
14 Finding nth child element of parent xpath=//body/*[3]/form/fieldset/*[2] Here, /*[3] describes the 3rd child element of body which is div[3]. Same way *[2] describes the 2nd child element of fieldset which is input[2]
15 Using Text() to find element xpath=//div/a[text()="English"] This will find a link that has text ="English"

Xpath examples for drop down

1. xpath=//body/div[3]/form/fieldset/select
2. xpath=//body/div[3]/form/fieldset/select[last()]
3. xpath=//body/div[3]/form/fieldset/select[@id='searchLanguage']
4. xpath=//body/div[3]/form/fieldset/select[@name='language']
5. xpath=//div[@class='search-container']/descendant::select[@name='language']
6. xpath=//select[contains(@id, "searchLanguage")]
7. xpath=//div[@class='search-container']/descendant::select[position()=1]
8. xpath=//body/div[3]/form/fieldset/select[count(*)>1]