

The meaning of XPath changes with the change in order. At last, select only the second employee the first employee element with the given attribute and and all the designation elements with given first and second both or | all the designation elements with either first or second, both attributes. Matching only those elements whose attribute and value are specified in the XPath all employee elements of given attribute and value. Selects all employee elements with a given id element with specified attribute value įinding employee elements with given attributes and values.

Selects name element of second employee element of empinfo element with attribute Selects the second employee of empinfo element. Opening and closing square bracket referring to a specific element sequence number (array sequence). Select all anchor elements that are children of h1 within a div element Select all anchor elements within h1 that are children of the div element Select the first two anchor elements within a div all anchor elements with class all anchor elements with class attribute and value ‘btn’ Selects the second-last anchor element within a div element Selects the last anchor element within the div element

Selects the first anchor element within the div element You can even use the relational and boolean operators with them. These predicate identifiers return the boolean values (true or false). They’re enclosed in square brackets, as shown in the table below. You can use predicates in XPath to locate a specific node containing a designated value. Selecting all tr elements that are children of div anywhere in the all class attributes Select all tr elements anywhere in the document Select all tr elements that are children of div Selects div element from the root element The following table includes XPath expressions for selecting nodes or WebElements. Gives the text of all the anchor href value of anchor tag Select all div all elements with ID all div elements with the name ‘post’ Here are some examples of various steps available in XPath: We can use prefixes in XPath at the beginning of the XPath paragraph tag from anywhere in the DOMįind all anchor (a) tags from the given node XPath uses the path expression for selecting nodes from XML and HTML documents.Īpplication find theaxis and corresponding steps Expression XPath expressions are patterns used to select a set of nodes and carry out transformations.

Select the parent of the current node the attributes Select elements in the document from the current element that matches the selection (no matter where they are) In order to select the nodes in an HTML document, XPath uses this path expression: are some popular Path expressions for selecting a node in an XML document: If there is any change to the page design or the DOM hierarchy, the existing XPath selector won’t be impacted. Mostly, the relative XPath is preferred for Selenium automation testing. To use the relative path, you must specify the double forward-slash (//) at the beginning. In this case, the element is positioned relative to its actual position. Unlike the absolute path, the relative XPath refers to an element that we want to locate in a specific location. In this case, we started from the HTML tag and traversed one by one to the div, containing the tag up to the final img tag.Įven though it seems simple, the common disadvantage is that even a small change in the DOM structure will lead to several automation failures. You will then see the Elements tab, where you can write the locator. To locate an element, you can right-click on the web element and click on Inspect. This indicates you’re selecting the element from the document’s root node.Įxample: /html//div/div/div/div/div/a/img Whenever you use the absolute path, you must begin the XPath using the single forward-slash (/). If there is any change made within the path of the element, it results in XPath failure. But, you might face issues using the absolute path. Absolute XPathĪbsolute path specifies the complete path from the root element to the element you want to use. In this section of our XPath cheat sheet, we shall look at different ways of using an XPath locator to find the desired WebElement in the DOM. One is through the absolute path, and the other is through the relative path. There are two ways to locate the desired WebElement in the DOM. You can find the desired WebElements from DOM using the appropriate web locator. It works as a map containing all WebElements and the one you are looking for. DOM is essential to navigating through HTML documents.
