Sie sind auf Seite 1von 6

X Path When a tester starts automating script for certain application and webpage the most tedious job

that he faced is the location of each object/Element that he uses in his automation script. Normally identification of object/element is divided mainly in three parts 1) Location based Identification 2) Cell based Identification 3) Property based identification Xpath is one example of Cell based Identification and this is further devided in four part 1) Assess the structure of the application pages and how best to locate a label on the page: Accessing certain structure of a web page is totally dependent on the type of application that we are using. Since we are taking webpage as the application so obliviously we will deal with HTML to access the structure of page

Username Password
Cancel Sign In

Suppose the html for above page is <Table> <tbody> <tr> <td> <span>Username</span></td> <td><input id=hfeb type=text<td> <span>Password</span></td> <td><input id=hjhflds type=text<td></tr>

</tbody> <table>

From here we can see that page contains table and labels like Username and Password which is mainly present in span tag without any special property so we can track this label username and password by its text so its Xpath would be //span*text()=Username+ Or we can write above xpath like this.

2) Identify the structural relationship between a label and its associated field: Now we should deal with the relationship with Label and Field and how they are related to each other. In our case Username and Text field comes in the same row and we are not using any cryptic and dynamic function to handle text field 3) Construction of Xpath to identify the element that we have to use in our Selenium script: Construction of xpath is also divided in 3 parts Locating the label Using the label to find the parent row element Find the desired child field of the row element The label can be located with the following XPath statement from Step 1: //span*text()=Username+ This XPath can then be modified to get to the parent row element. To do this, it is important to note that the row (TR) element is not the direct parent of the Span element. The column element represented by the td tag is the direct parent of the Span element. The tr element is the parent of the td element. Therefore, to get to the tr element, we must travel two levels up the parent hierarchy. This can be done with the following statement: //span*text()=Username+/../.. Adding /.. moves the reference up one level from the SPAN element (the label) to

the SPAN elements direct parent. Sincethe parent row element (TR), we now need to update the statement to get the child textbox of that row, which is represented in the HTML by the input tag. This is done by modifying the previous XPath statement to appear as follows: //span*text()=Username+/../..//input Since there is only one input element that is a child of the row element, there is no need to use any properties to further we need to go up two levels, we can do this with /../... Once we have access to indentify the input element. 4) Using Xpath in appropriate place in script: Since we are now familiar with the xpath of Username and password like this driver.findElement(By.xpath(// span[text()=Username+/../..//input))

Few more work out example to learn xpath


Example 1: /AAA This xpath says : Select all root AAA
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA> Example 2: /AAA/CCC This Xpath says : Select all child CCC of root AAA <AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA> Example 3: /AAA/DDD/BBB This xpath says Select all BBB which are children of DDD and which is further children of root AAA

<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA> Example 4:

//BBB This xpath says to select all AAA


<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>

Example 5: //DDD/BBB This Xpath says Select all BBB which is the children of DDD
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>

Example 6: /AAA/DDD/* This Xpath says Select all element which are enclosed by /AAA/DDD

Example 7: //* Select all elements in the HTML file Example 8: /AAA/BBB[1] This xpath says select first Chil BBB of AAA <AAA> <BBB> <BBB> <BBB> <BBB> </AAA> But to select the last BBB child of AAA We can write /AAA/BBB[Last()] Example 9:
<AAA> <BBB id = "b1"/> <BBB id = "b2"/> <BBB name = "bbb"/> <BBB/> </AAA> //@id This xpath says: Select all Id attribute

//BBB[@name] This xpath says: select all BBB with attribute name
<BBB name = "bbb"/> If we want to select all BBB in above html Then xpath will be //BBB[@*] If we want to select only one BBB that dont have any attribute in above html then //BBB[not(@*)]

If we want to select some specific BBB with its attributes value then

//BBB*@name=bbb+ Then it will select BBB where value of its attribute is bbb Then in above HTML <BBB name = "bbb"/>

Das könnte Ihnen auch gefallen