Identification using XPath is another locator identification strategy supported by selenium. XPath is query language used for locating nodes within an XML document. Since HTML can be considered to be an implementation of XML, XPath can be used for locating the web elements present within the HTML of the web application. Like CSS, XPath can also locate almost any element within a web application (even those without class, name or id attribute).
I shall demonstrate some of the common XPath locator identification methods through an example. For this take up the Wikipedia-login page. We shall try to describe the user name field on the page using the common XPath identification methods.
Identification using absolute path: xpath=/html/body/div[3]/div[3]/div[3]/div/div/form/div/input
Identification using relative path: xpath=//input[@id="wpName1"]
Identification using any attribute: xpath=//input[@class="loginText"]
Identification using multiple attributes: xpath=//input[@id="wpName1"][@class="loginText"]
Identification using n-th child: xpath=//div[@id="userloginForm"]/form/div[1]/input
These example cover some of the basic locator strategies using XPath. In order to learn more check out W3Schools XPath Tutorial and W3C XPath Recommendation
From the above examples it can be seen that unlike the id and name locator strategies, constructing XPath strategies are much more complex owing to the various rules to be considered. However using the firebug tool they can constructed relatively easily.
Using firebug to create XPath locators
STEP1: Use firebug to inspect the particular UI element to be used.
STEP2: Right click on the element’s HTML tag and use the ‘Copy XPath’ option.
STEP3: Now paste the XPath locator in the selenium IDE and place a “/” in front. Now using the find button we can locate the object.
Although XPath locators can be used to describe almost any UI element on a web page compared to the other locator strategies it is the slowest at object identification. So it is highly recommended to use XPath only when you are not able to locate using the other locator strategies (id, name, class, CSS, etc.).
No comments:
Post a Comment