Wednesday, December 22, 2010

Reverse XPath

Finding the xpath of a element using bottom up approach

Assume that we need to find the xpath of a link "Login" appearing in a webpage. Usually when we find xpath of a GUI element, we normally follow a top-down approach, meaning the xpath is recorded from the parent level to the child. But there is also a bottom up approach, where in you can begin from a inner child element and navigate all the way up till the GUI element is reached.

Below is the corresponding xml code and various method of finding XPath of "Login" link. The bottom-up approach is explained by note (e) in the below table.

Sample XML Code
XPath

(a) Using Top Down approach starting from a top node
//table/tr/a[text()='Login']

(b) Directly accessing the child node using its text value
//a[text()='Login']

(c) Directly accessing the child node using child's attribute value
//a[@href='1.html']

(d) Directly accessing the child node using wildcard search. All occurences of text "Logi*" is matched by below xpath.It will work in this example since there is only one text "Login" which has occurence "Logi" in it.
//a[contains(text(),'Logi')]

(e) Using Bottom Up approach starting from another child node and navigating to the actual node. Note: Here the DOM structure should be maintained (as it is in the xml) between the start node and end node
//a[text()='Home']/../../../table/tr/a[text()='Login']


Thanks

No comments:

Post a Comment