Thursday, July 19, 2012

Selenium Module #4

 X  Path

 In Selenium Module#3 I mentioned an example. Now the question arises how selenium specifically identifies which element to click , where to type. The answer to that question is target. It uniquely identifies the object on page. For that you need X Path. You can get this through Firebug. Firebug is also an add on in Firefox like Selenium IDE.

Installing Firebug and Firepath:
In Mozilla Firefox, go to Tools -> Add- ons -> Search Firebug.
It will be displayed in search result. So click on Install button and install it. After installing firebug, you need to install Firepath. Firepath is an extension of Firebug which will show the path of the web element.
For installing Firepath go to and search Firepath. Click on the first link displayed in search results and click on Add to Firebug. Restart your browser. Now your firepath is installed.
For checking whether its installed or not go to Tools -> Web developer -> Firebug -> Open Firebug.
It will open on your browser. And you will see Firepath is installed over Firebug.

Below is the screenshot how it looks like.

How to use Firepath?

 Lets take a simple example of gmail login.
Go to the website Here you see the user name and password textbox, Sign in button. Now for knowing the target or xpath of these elements, we will use firepath.
Open firebug from tools.
On the left side of the firebug user interface, there will be an arrow icon to Inspect Element ("Click an element in the page to inspect"). Click on this inspect icon. Now move your mouse over the gmail page. You will see that it is inspecting the page objects. Now click on username textfield. It will display the source of username field and in xpath textfield on the top of firebug, it will display the path. You can see below in the screenshot.

Here xpath is //*[@id='Email']. ID parameter is used to identify the username field as it uniquely identifies it. And this xpath will be used in Selenium IDE and RC. Any other parameter can also be used but it should uniquely identify that particular web element.

This xpath is relative. For absolute xpath i.e for knowing the path from the root, click on Firepath drop down and check on 'Generate Absolute Path' and absolute xpath will get generated. However it does not make any difference whether we are using relative or absolute xpath as both of them points to the same element.

XPath is mostly used used by developers to find out the exact location of the error in their program. But nowadays automation engineers are also using it.

No comments:

Post a Comment