WORKING WITH QTP

TestEveryThinG

Locating by XPath

Posted by rajivkumarnandvani on September 20, 2009

Locating by XPath

XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. XPath extends beyond (as well as supporting) the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.

One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an id or name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.

Since only xpath locators start with “//”, it is not necessary to include the xpath= label when specifying an XPath locator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 <html>
  <body>
   <form id="loginForm">
    <input name="username" type="text" />
    <input name="password" type="password" />
    <input name="continue" type="submit" value="Login" />
    <input name="continue" type="button" value="Clear" />
   </form>
 </body>
 <html>
  • xpath=/html/body/form[1] (3) – Absolute path (would break if the HTML was changed only slightly)
  • //form[1] (3) – First form element in the HTML
  • xpath=//form[@id='loginForm'] (3) – The form element with @id of ‘loginForm’
  • xpath=//form[input/\@name='username'] (4) – First form element with an input child element with @name of ‘username’
  • //input[@name='username'] (4) – First input element with @name of ‘username’
  • //form[@id='loginForm']/input[1] (4) – First input child element of the form element with @id of ‘loginForm’
  • //input[@name='continue'][@type='button'] (7) – Input with @name ‘continue’ and @type of ‘button’
  • //form[@id='loginForm']/input[4] (7) – Fourth input child element of the form element with @id of ‘loginForm’
  • Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.
/AAA/BBB[1]
Select the first BBB child of element AAA
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
/AAA/BBB[last()]
Select the last BBB child of element AAA
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>

Attributes are specified by @ prefix.

//@id
Select all attributes @id
<AAA>
<BBB id = “b1″/>
<BBB id = “b2″/>
<BBB name = “bbb”/>
<BBB/>
</AAA>
//BBB[@id]
Select BBB elements which have attribute id
<AAA>
<BBB id = “b1″/>
<BBB id = “b2″/>
<BBB name = “bbb”/>
<BBB/>
</AAA>
//BBB[@name]
Select BBB elements which have attribute name
<AAA>
<BBB id = “b1″/>
<BBB id = “b2″/>
<BBB name = “bbb”/>
<BBB/>
</AAA>
//BBB[@*]
Select BBB elements which have any attribute
<AAA>
<BBB id = “b1″/>
<BBB id = “b2″/>
<BBB name = “bbb”/>
<BBB/>
</AAA>
//BBB[not(@*)]
Select BBB elements without an attribute
<AAA>
<BBB id = “b1″/>
<BBB id = “b2″/>
<BBB name = “bbb”/>
<BBB/>
</AAA>

Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space.

//BBB[@id='b1']
Select BBB elements which have attribute id with value b1
<AAA>
<BBB id = “b1″/>
<BBB name = ” bbb “/>
<BBB name = “bbb”/>
</AAA>
//BBB[normalize-space(@name)='bbb']
Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison
<AAA>
<BBB id = “b1″/>
<BBB name = ” bbb “/>
<BBB name = “bbb”/>
</AAA>

Function count() counts the number of selected elements

//*[count(BBB)=2]
Select elements which have two children BBB
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
</CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
<EEE>
<CCC/>
<DDD/>
</EEE>
</AAA>
//*[count(*)=2]
Select elements which have 2 children
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
</CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
<EEE>
<CCC/>
<DDD/>
</EEE>
</AAA>

Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.

//*[name()='BBB']
Select all elements with name BBB, equivalent with //BBB
<AAA>
<BCC>
<BBB/>
<BBB/>
<BBB/>
</BCC>
<DDB>
<BBB/>
<BBB/>
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>
//*[starts-with(name(),'B')]
Select all elements name of which starts with letter B
<AAA>
<BCC>
<BBB/>
<BBB/>
<BBB/>
</BCC>
<DDB>
<BBB/>
<BBB/>
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>
//*[contains(name(),'C')]
Select all elements name of which contain letter C
<AAA>
<BCC>
<BBB/>
< BBB />
< BBB />
</BCC>
<DDB>
< BBB />
< BBB />
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>

The string-length function returns the number of characters in the string. You must use &lt; as a substitute for < and &gt; as a substitute for > .

//*[string-length(name()) = 3]
Select elements with three-letter name
<AAA>
<Q/>
<SSSS/>
<BB/>
<CCC/>
<DDDDDDDD/>
<EEEE/>
</AAA>

Several paths can be combined with | separator.

//CCC | //BBB
Select all elements CCC and BBB
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>
/AAA/EEE | //BBB
Select all elements BBB and elements EEE which are children of root element AAA
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>
/AAA/EEE | //DDD/CCC | /AAA | //BBB
Number of combinations is not restricted
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>

Note :–  All Example are taken from http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Hi i find something more on x path from one group site

<td id=”zippy”>One</td>
<td id=”zippy”>Two</td>
<td id=”zippy”>Three</td>

browser.cell(:xpath, “//td[.='Three']“)

The dot being compared to ‘Three’ represents the text of the cell.

And if the cell text has leading or trailing whitespace, you can strip it from the text before doing the comparison:

browser.cell(:xpath, “//td[normalize-space(.)='

Three']“)

About these ads

9 Responses to “Locating by XPath”

  1. One Two

    get next sibbling

    //td[.='One']/following-sibling::*
    it will select the Two

  2. The code below worked in Ruby 1.8.6 and Watir 1.6.2

    @what = ‘/Administration/DeleteFilter.asp’
    ie.element_by_xpath(“//tr[contains(@onclick, '#{@what}')]/”).click()

  3. Just a quick thanks, as this article has just saved me a bit of time! Just starting out with Selenium and needed to use By.XPath(…) and this article just reminded me that my problem wasn’t with the command, rather I’d forgotten the XPath syntax!

  4. Thanks for reading and comment.

  5. Anonymous said

    I’m just starting with Selenium and a colleague mentioned XPath. This looks like a great place to start.

    Thanks.

  6. Thanks

  7. shobit said

    Hello Rajiv, can we access the java script related elements using XPATH ? If yes , please guide. Thanks for the great article

  8. shobit said

    Here is a quick snippet of java script element in HTML page. I want to access ‘qt’:’requiredelementid’ via XPATH

    var TREE_ITEMS = [
    ['FROZEN', null,{'hs':'setPKTree','hs_param':'requiredelementid','qt':'requiredelementid'},
    ],

  9. Pradeep said

    Hi Rajiv,
    i am working on a dot net based application, if i spy, QTP doesn’t
    identify as webtable or frame but webelement. but i want to verify
    the follwing things in a customer application form whic has two sections i.e
    1. Applicant 2.Co-Applicant:

    1.verify that Middle name edit field is present in between first and last name field
    2.verify that the page has two sections i.e Applicant and Co-applicant
    3.verify Applicant section contains the field like first name, middle name,
    last name, address etc.

    Please guide me how to resolve this problem……….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 147 other followers

%d bloggers like this: