This shows you the differences between two versions of the page.
en:xpath [2018/01/08 13:14] tast |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== XPath object identification ===== | ||
- | |||
- | {{:TAST-icon.png?nolink&70|}} 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 **both** "//b//" 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//"| | ||