Ancestor: contains the ancestors of the context node – //Tag-name//ancestor::node().XPath has a total of 13 different axes, here we will look at some popular ones. An XPath axis tells the XPath processor which “direction” to head in as it navigates around the hierarchical tree of nodes. These XPath methods using axes are used to find the complex or dynamic elements. Taking same example of ‘Sign In’ button at Google XPath methods using axes Start-with XPath method matches the starting text of the attribute to find the element whose attribute changes dynamically. Say the ID of a particular element changes dynamically like message12, message 576, message 1938 and so on…but if you notice, it starts with a particular text. OR: Statement is true even if any one of the condition is trueĪND: OR Start-with | Locating elements when starting text is known.Using any of these attributes we can generate a corresponding XPath using AND / OR. Taking above example of email text box at Gmail login page, we can use id and name attributes’ value. Why do you think we have First name + Last name to recognize a person without any confusion ? Sometimes it may not be possible to locate an element with a single attribute. Using OR & AND | Locating Elements with Multiple Attributes Using partial text ‘Sign’ on the button, we can generate a corresponding Xpath method is particularly helpful for locating dynamic values whose some part remains constant. Please note that the text is case sensitive. In that case we can use the contains() XPath methodto find the element with partial text match. But some part of the text (partial text) remains fixed. Dynamic in the sense it changes with every page refresh. Say you are generating an XPath method using visible text, but the text is dynamic. Contains() | Locating Element using part of the visible text Locating by the visible text is not advisable if you are testing a multilingual application OR same text is appearing in more than one location. Using visible text ‘Sign in’ on the button, we can generate a corresponding XPath method.ĭriver.findElement(By.xpath(“//a”)) Please note that the text is case sensitive.įor example, if you inspect Sign in button at .in page, For this we can use the XPath text() method to find the element with exact text match. Say you want to identify element with the text displayed on screen. Using any of these attributes we can generate a corresponding Text() | Locating Elements with Known Visible Text We can find unique id or name attributes’ value. to identify the example, if you inspect email text box at Gmail login page, Basic XPath | Locating Elements with Attributesīasic XPath methods use other unique attributes like ID, Name, Class name, etc. For cases like these, XPathprovide different methods of locating elements like – using the text written over the elements using element’s index using partially matching attribute value by moving to sibling, child or parent of an element which can be uniquely identified etc. Many a times in automation, we either don’t have unique attributes of the elements or the elements are dynamically generated with the attribute’s value not known beforehand. In this article we will explore some powerful capabilities of XPath methods which open up many new methods for locating complex and even dynamic elements which in the end, make your scripts easier to maintain. We learnt what is XPath, its syntax and types. mastering XPath methods or CSS is essential for the Selenium test automation. In our earlier post ‘ Introduction To XPath | Selenium XPath Examples’ we learnt that XPathis one of the most popular element locator technique in Selenium along with CSS selector, i.e.