Google+

332. XPath Functions






As XPath is a programming Language it has many built in functions which we can use in XPath Statements.

Automation using selenium is a great experience as it provides many ways to identifying an object or element on the web page. But sometime we face the problems of identifying the objects on a page which have same attributes and we can no where distinguish them.

For example, view the following code

<html>
<body>
<input type='checkbox' name='chk'>first
<br><input type='checkbox' name='chk'>second
<br><input type='checkbox' name='chk'>third
<br><input type='checkbox' name='chk'>forth
<br><input type='checkbox' name='chk'>fifth
<br><input type='checkbox' name='chk'>sixth
</body>
</html>

Here in this example, we've 6 check box options which are defined using <input> html  tag and also these html tags contain the same attributes  i.e. type='checkbox' and name='chk' for all the 6 check box options. If we have a requirement of locating the fifth check box using XPath, you have to distinguish it from other check box options using XPath options like Index or Functions.

Using XPath Index

We have already used Index in our previous posts.

Example: //p[2]

Here 2 is the index value. On giving two our XPath will locate the second <p> html tag from all the available <p> tags in the HTML Source code.

In this Check box options case, your XPath path may look like //input[5] to locate the 5th <input> html tag (i.e. locating the 5th check box option).

Using XPath Functions

We can also use XPath predefined Function to resolve the above case.

In this Check box options case, you can use the XPath predefined Function position( ) to create XPath Statement  //input[position( )=5] to locate the 5th <input> html tag (i.e. locating the 5th check box option).

There are many other predefined functions in XPath which can help us in locating the elements in critical cases.


Please comment below to feedback or ask questions.




last( ) - XPath Function will be explained in the next post.





No comments: