Examples of Relative and Absolute XPath in Selenium

1. Relative XPath:

Suppose we want to locate the “Search” input field within the navigation bar of a webpage. The Relative XPath expression could be:

//img[@class='gfg_logo_img']

This XPath starts from the ‘nav‘ element and searches for the input field with the id “search” within it. It’s flexible because it doesn’t depend on the exact position of the navigation bar within the document.

Example:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;

public class Xpath_Selection {

   public static void main(String[] args) {                       

      System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe");                       

      WebDriver driver = new FirefoxDriver();                       

      driver.get("https://www.stqatools.com");                    

      // Relative xpath
     driver.findElement(By.xpath("//img[@class='gfg_logo_img']"));  
     }
}

Output:


Relative XPath


2. Absolute XPath:

To find the same “Search” input field using an Absolute XPath expression:

//html[1]/body[1]/nav/div/a[2]

This Absolute XPath specifies the full path from the root of the HTML document to the input field. It relies on the exact structure of the webpage, making it longer and more prone to breaking if the structure changes.

Example:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;

public class Xpath_Selection {

   public static void main(String[] args) {                       

      System.setProperty("webdriver.firefox.marionette", "C:\\geckodriver.exe");                       

      WebDriver driver = new FirefoxDriver();                       

      driver.get("https://www.w3wiki.org");

      // Absolute xpath
      driver.findElement(By.xpath("/html[1]/body[1]/nav/div/a[2]"));                       
     }
}

Output:


Absolute XPath Output



Difference between Relative and Absolute XPath in Selenium

XPath is important for element location in Selenium automation. With flexibility, Relative XPath navigates elements according to how they relate to other elements. While absolute XPath offers greater precision, it makes scripts larger and less flexible because it provides the entire path from the HTML document’s root. Comprehending the differences between Absolute and Relative XPath is essential to effective automated programming. This article explores their differences and advises when to use each technique for reliable Selenium automation.

Table of Content

  • Relative XPath in Selenium
    • Advantages of Relative XPath
    • Disadvantages of Relative XPath
  • Absolute XPath in Selenium
    • Advantages of Relative XPath
    • Disadvantages of Relative XPath
  • Choosing the Right XPath Strategy
  • Examples of Relative and Absolute XPath in Selenium
    • 1. Relative XPath:
    • 2. Absolute XPath:
  • Best Practices for Writing XPaths
    • Best Practices for Relative XPath:
    • Best Practices for Absolute XPath:
    • Related Articles:
  • Conclusion
  • FAQs on Difference between Relative and Absolute XPath in Selenium

Similar Reads

Relative XPath in Selenium

Relative XPath in Selenium refers to locating elements on a webpage based on their relationships with other elements, starting from a specific context node. Unlike Absolute XPath, which provides the full path from the root of the HTML document, Relative XPath offers more flexibility and simplicity. It is often preferred for automation testing as it adapts well to changes in the webpage structure and allows for shorter and more maintainable XPath expressions. Relative XPath expressions typically use attributes, tags, and positional relationships to identify elements efficiently....

Absolute XPath in Selenium

In Selenium, Absolute XPath is a technique for finding components on a webpage by giving their precise path from the HTML document root. It gives the entire element’s hierarchical path, from the top-level HTML node to the target element. This path leads to the target element and contains all of the parent nodes, divided by slashes (/). Although Absolute XPath can be more accurate when locating items, it is less flexible and typically longer than Relative XPath, which increases the likelihood that it will break when the structure of the webpage changes....

Choosing the Right XPath Strategy

Choosing the right XPath strategy is crucial for effective and robust Selenium automation. Here’s a guide to help you make the right choice:...

Examples of Relative and Absolute XPath in Selenium

1. Relative XPath:...

Best Practices for Writing XPaths

Writing XPath expressions in Selenium efficiently requires adherence to best practices. Here are guidelines for crafting both Relative and Absolute XPath expressions:...

Conclusion

In conclusion, understanding the differences between Absolute and Relative XPath is crucial for effective Selenium automation. While Absolute XPath offers precision, it’s less flexible and prone to breakage. On the other hand, Relative XPath provides flexibility and adaptability, making it preferred for most automation tasks. By following best practices and choosing the right XPath strategy based on your project’s needs, you can create robust and maintainable Selenium scripts. Remember to regularly review and update XPath expressions to accommodate changes in webpage structure, ensuring smooth and reliable automation....

FAQs on Difference between Relative and Absolute XPath in Selenium

What is the difference between Absolute and Relative XPath in Selenium?...

Contact Us