Identifying XPath of a element using XPath Query

Below are some of the common xpath query which you will need while doing your selenium automation. Using xpath query, your selenium testing much stable since the slightest change of the GUI wont break your automation.

GUI Element
Corresponding XPath
Remarks
<input type="submit" value="Save"/> //input[@value="Save"] Exactly matches the text "Save"

<input type="submit" value="Save1"/>

<input type="submit" value="Save2"/>

(//input)[1]   matches the 1st input element, "Save1"

(//input)[2]   matches the 2nd input element, "Save2"


<a href="somelink">Structure</a>
//a[text()="Structure"]
Exactly matches the value within a given tag. Here the text "Structure" which is in between the <a> tag (anchor tag)
<a href="somelink">x_structure_y</a> //a[contains(text(),'structure')]
Like wildcard search, matches the input element which has the text "structure" within a given tag
<input type="button" name="ccd_ccd_81533739_removeevent"/>

//input[contains(@name,concat("ccd","_","ccd","_")) and contains(@name,"removeevent")]

Note: The above xpath can be used when the attribute value has a part which is dynamic in nature (here in this case the text in between "ccd_" and "_removeevent" '81533739" keeps on changing everytime the GUI element is accessed)

Matching multiple text(s) pattern of a attribute value
<input type="text" value="Search..."/>
//input[@type='text' and contains(@value,'Search')]
Matches based on multiple attributes of a GUI element . Here the xpath of the text box is derived  based on its 'type' and 'value' attributes
Comments:

Great start,you have clearly mentioned how to use core xpath() functions.You can keep this list growing.:-)

Posted by sriram on July 08, 2008 at 10:59 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

I'm a Quality Engineer at Sun Microsystems. Here in this blog, you may find information about Software Testing, Portal Server, Virtualization, Web2.0 and Misc Technical topics. The views expressed here are personal and does not reflect that of my employer

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today