Different ways to create Xpath

XPath (XML Path Language) is a powerful language used to navigate through elements and attributes in an XML document, which is also widely used in web scraping and automation testing with Selenium to locate elements in HTML documents. Here are different types of XPath expressions and how you can create them:

  1. Absolute XPath: This provides the complete path from the root element to the desired element. It is generally brittle and not recommended for most applications as it can easily break with any small change in the HTML structure.
  2. Example: /html/body/div[1]/section/div[1]/ul/li[3]/a
  3. Relative XPath: This starts with the double forward slash //, which means it can search the element anywhere in the document. This is more flexible and commonly used in Selenium.
  4. Example: //div[@class='example-class']
  5. Using Attributes: You can locate an element by its attribute using @.
  6. Example: //input[@name='username'] (finds an <input> element with the name attribute ‘username’)
  7. Using Contains: This function is used when the attribute value is dynamic or partially known.
  8. Example: //*[contains(@class, 'button')] (finds any element whose class attribute contains the word ‘button’)
  9. Using Starts-With: This function is helpful when the attribute value starts with some specific characters.
  10. Example: //input[starts-with(@id, 'user-')] (finds <input> elements whose id attribute starts with ‘user-‘)
  11. Using Text: To find elements with a specific text.
  12. Example: //a[text()='Click Here'] (finds <a> elements exactly containing the text ‘Click Here’)
  13. Example: //*[contains(text(), 'Click')] (finds elements containing the text ‘Click’)
  14. Using Axes: XPath axes such as following, preceding, ancestor, descendant, etc., can help find elements based on the relationship with another element.
  15. Example: //div[@class='login']/descendant::input (finds <input> elements that are descendants of a <div> with class ‘login’)
  16. Example: //span[@id='username']/following-sibling::input (finds <input> elements that are siblings following a <span> with id ‘username’)
  17. Using Index: If there are multiple elements with the same XPath, you can use an index to select a particular element.
  18. Example: (//input[@type='checkbox'])[2] (selects the second checkbox input on the page)
  19. Combining Conditions: You can combine more than one condition using and or or.
  20. Example: //input[@type='submit' and @value='Search'] (finds <input> elements with type ‘submit’ and value ‘Search’)
  21. Using Wildcards: For matching part of an attribute or tag.
  • Example: //*[starts-with(name(), 'us')] (selects elements whose tag name starts with ‘us’)

These are some common methods for creating XPath locators in Selenium. Depending on the complexity of the HTML structure and the reliability required, you may choose one or a combination of these methods to achieve the best results.

Leave a comment

Your email address will not be published. Required fields are marked *