Automatic XPath Construction
Any element on a web page may be found by its XPath. Moreover, it is possible to locate the same element using a number of different XPath statements. For example:
All three may point to the same element.
By default Rapise will construct an XPath statement meeting the following requirements:
- Universal: should work on different browsers equally.
- Short: whenever possible try to locate an element by its unique attributes. I.e. use:
instead of the full path
- XPath is able to search for any element by its index, location and attributes. However it is impossible to use regular expressions as a part of XPath statements. In most cases this limitation is not critical. XPath itself allows using set of functions covering possible needs for searching an element in an advanced manner.
- XPath is a tool for finding the location of an element based on the Document Object Model (DOM) within a web browser. The DOM representation of a web page differ in some aspects between browsers. In many cases this difference is not critical and may be ignored by use of proper XPath statements.
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:
Selects all child nodes of the named node
Selects from the root node
Selects nodes in the document from the current node that match the selection no matter where they are
Selects the current node
Selects the parent of the current node
In the table below we have listed some path expressions and the result of the expressions:
Selects all the child nodes of the bookstore element
Selects the root element bookstore
Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!
Selects all book elements that are children of bookstore
Selects all book elements no matter where they are in the document
Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
Selects all attributes that are named lang
Browser-specific XPath Implementation
- Firefox has a native XPath implementation
- Chrome has a native XPath implementation
Working with Framesets
Web pages sometimes use HTML frames. The XPath works inside the frame contents. We use following syntax to combine multiple XPath statements into a single line:
The special statement:
Is used as a separator for XPath statements pointing to constituent frames.
The top-level frame is found by name 'main'
Then the frame's contents are searched for the third <a> element (i.e. 3rd link on a page).
Working with Data Tables
Web pages will often represent the results of searches and queries as formatted tables. As described in the HTML tables guide, you can use XPath to make the verification of such results much easier.
Using the Rapise Web Spy
Each major browser provides a number of standard ways to check the DOM in addition to available plugins and extensions. In addition, since Rapise v3.1, Rapise provides its own powerful WebSpy tool that can inspect any of the web browsers it supports.
To open up the Web Spy, open up Rapise and under the main Spy ribbon icon, select 'Web Spy'. Then click on the Web Spy in the ribbon or on the main recording activity dialog box. This will display the Rapise web spy:
You can learn about using the Rapise Web Spy in this article.
API for DOM Manipulation
Upon learning a web element in Rapise, you get an object of type HTMLObject . Each HTMLObject provides set of functions to facilitate the cross-browser access to web element parents and children.
Manipulating and Checking XPath
There are a number of Firefox plugins that may help you to construct and verify your XPath statements:
Similarly for Google Chrome, there are several plugins: