Advanced Selenium
XPath is used to navigate through elements and attributes in a document. XPath includes over 100 built-in functions. Functions for: ◦ string values ◦ numeric values ◦ date and time comparison ◦ node manipulation ◦ sequence manipulation ◦ Boolean values ◦ … and more
Absolute xpath is very fragile. Any elements inserted into the page above the selected element, will break the code Some pages utilize dynamically generated elements. These elements may be added during user interaction with the AUT, and have varying ids, and location.
Try to include id's in your code and work relative to them If you cannot get or use an id (because it is undefined or dynamically assigned), try selecting an attribute unique to given element and work from there. Also, you can utilize the text value of the node to locates the element
Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets of the xpath. Selects attributes last() Selects the last element position() Selects the node at the defined position text() Selects the text contains() Can be used with text() to select nodes based on their text value starts-with(st1, st2) Can be used with text() to select nodes based on their text value ends-with(st1,st2) Can be used with text() to select nodes based on their text value
ExpressionDescription node[last()]Selects the last matching node node[last()-1]Select one to last matching node node[position()=1]Selects the node at position 1 node[position()<3]Selects the first 2 nodes a node with attribute “attr” a node with an attribute “attr” whose value is ‘big’ node[sub_node>35.00]Selects a sub-node of the node with text value more then 35 node[contains(text(), ‘dog’)]Selects a node which contains text “dog”
ExpressionDescription node//subnodeSelects all sub-node elements that are descendant of the node element, no matter where they are under the node element a node with id ‘apple’ and size of 40 the child link of the sub- node of the node with id ‘b12’
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node childSelects all children of the current node descendantSelects all descendants (children, grandchildren, etc.) of the current node followingSelects everything in the document after the closing tag of the current node following-siblingSelects all siblings after the current node parentSelects the parent of the current node precedingSelects everything in the document that is before the start tag of the current node preceding-siblingSelects all siblings before the current node
ExpressionDescription child::sub_zeroSelects all sub_zero nodes that are children of the current node child::*Selects all children of the current node child::text()Selects all text child nodes of the current node descendant::sub_zeroSelects all sub_zero descendants of the current node ancestor::sub_zeroSelects all sub_zero ancestors of the current node child::*/child::sub_nodeSelects all sub_node grandchildren of the current node