xpath_object_identification [TAST DokuWiki ]

User Tools

Site Tools


Sidebar

First steps in TAST

What is UML

Computer setup for TAST use

FAQ

Recognized Issues

TAST tool Menu

TAST Adaptors

Adaptors examples

TAST Integrations

Interesting features

Documentation of technical administration

Modeling recommendations

Training in the TAST tool

xpath_object_identification

XPath object identification

In the following lines, the user will find tips for being able to identify the web objects via XPath.

XPath is a way of objects identification based on the path where the element is nested in. As an example, the XPath ”/html/body/div[2]/div/div/footer/section[3]/div/ul/li[3]/a” will search in the html body an element “div[2]”; inside this it will look for a “div”; inside this, another “div”, and then a “footer” and so on and so on…

There are, basically, two different ways to use the XPath, using the complete path, or finding a partial path (for this, instead of opening the sentence with ”/”, they are open with ”/ /”). As always, in testing, it is really important to make sure that the Xpath that we write is unique and represents one element with no possibility of error. As a little reminder, notice that the ID attribute is unique.

Nevertheless, since this way can be too long, there are some ways to shorten it. For this, is necessary to know some syntax related to it:

XPath Expression Description Expression example Example meaning
/Selects from the root node. (…)/b/a Element ”a” child of ”b”, child of ”(…)”.
/ /Selects any nodes from the current node that match the selection no matter where they are. b / / a(…)All the ”a(…)” elements child of ”b”.
* Matches any element node (wildcard). b / * / a Any ”a” element whose grandparent is ”b”.
. Selects the current node. ./ /de Any ”de” element that is one or more levels inside the current node.
.. Selects the parent of the current node. abc[../mg] Any ”abc” element that has a ”mg” sibling.
@ Selects attributes. a[@bcd] a” element with a ”bcd” attribute.
@ = Selects attributes with a value. a[@bcd=“hola”] a” element with a ”bcd” attribute equal to hola.
@* Matches any attribute node. a[@*=“hola”] a” element with any attribute equal to hola.
last() Matches the last element. b / a[last()] The last ”a” element child of ”b”.
[] Element that contains something. c[hello] / b[@a] b” element that contains an ”a” attribute, child of a ”c” that contains a hello element.
[][] Element that contains two things (or more, if more brackets are added). a[@b][@c] a” element with both a ”b” attribute and a ”c” attribute.
[n] The n-th element. b/a[3] The 3rd ”a” element child of ”b”.
!= Not equal. a[@bcd!=“hola”] a” element with a ”bcd” attribute NOT equal to hola.
last() Last element. b/a[last()] the last ”a” that is child of a ”b”.
() Groups operations for establishing the priorities. (de/mg)[3] the third entire set ”de/mg”.
and Logical AND as usual. a[b and c] a” element containing bothb” and ”c”.
or Logical OR as usual. de[@mg or @dus] de” element containing a ”mg” attribute or a ”dus” attribute.
contains() Matches all the elements that (partially) contains what is inside the brackets.de[contains(.,'click here')] de” elements with a text containing ”click here”.
starts-with() Matches all the elements that start with what is inside the brackets. de[starts-with(@id,'id-name')] de” elements whose id starts with ”id-name(…)”.
[ .=''] Matches the elements with a string. de[.='string 1 of 6'] de” elements with the string ”string 1 of 6”.
normalize-space() Matches the elements with a string, ignoring leading or trailing blank spaces. de[normalize-space()='mg'] de” elements with the string ”mg” or ” mg” or ”mg ” or ” mg ”.
text() Selects the elements with a specific text within. de[text()=“mg”] de” elements with the string ”mg” in the text field.
[ / @ ] Selects the elements that have specific children. / / de[mg/@attr1='nrw']de” elements which have a ”mg” child with an attribute ”attr1” equal to ”nrw”.


How to Obtain Xpath in a XML document

How to obtain the Xpath of the XML node with Notepad ++:

1. Inside Notepad++ go to Plugins then go to Plugin Manager and click in Show Plugin Manager.
2. Install XML Tools.
3. Restart Notepad++.
4. Load your XML document.
5. Place your cursor on to the node you are looking to generate the Xpath.
6. Go to plugins then XML Tools and select Current XML Plath (Default Hotkey: Ctrl + Alt + Shift + P).

Then you have a copy of the Xpath in the clipboard.

xpath_object_identification.txt · Last modified: 2023/12/18 14:45 (external edit)