XPATH Usage in Selenium

Recommended by 0 users


There are times that you will need to find elements that are the same except for the difference in one or two attributes. To handle this, we can add the attributes to the query so that we can try to make the element more unique for use in the test. The format can be used for any attribute on any element.

It will always follow the xpath=//element [@attribute=’attribute value’].

For example, if you have two div elements on the page but they only differ by the class attribute, your XPath query would look like


Xpath usage:-

1)  Two object has same attributes

<input id =123 name =456> textbox1
<button id =123 name =456>textbox2

2)  The object attribute changing dynamically–facebook

3) Element has no attributes.



//–parent element /child element

//element[@attribute=’attribute name’]

Example 1:-

<input id =123 name =456> –textbox

<button id =123 name =456> –button


Example 2:-

<span id=456>

<input id =123 name =456> –textbox <input id =123 name =456> –textbox //span[@id-‘456’]/input[2]

Face book code:-

<input type=”submit” id=”u119310_3″ tabindex=”4″ value=”Log In”> //input[@type=’submit’ and value=’Log In’]





ocat=707″ class=”current”>Travel Gear</a>

Using attribute:-


Using Prent element:-

//dd[@class=’last odd’]/ol/li[3]/a

Example for Link:-

<a href=”http://www.rightstart.com/search/result/?link_location=default.leftnav&amp;ocat=707″
class=”current”>Travel Gear</a>

//a[@class=’current’] –its not working


Xpath using position:-

//element[@att=’attname’]/element1/element2 //dd[@class=’last odd’]/ol/li[10]/a

Dynamic element:-

<input type=”submit” id=”u075656_3″ tabindex=”4″ value=”Log In”>  –button whihc id is dynamic


//input[@type=’submit’ and @value=’Log In’]

Enroll for Instructor Led Live Selenium Training

0 Responses on XPATH Usage in Selenium"

Leave a Message

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

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.