Absolute xpath


An absolute XPath expression provides the complete path from the root of the document to the desired element. While absolute XPath expressions are less recommended than relative XPath expressions (as they are more prone to breaking when the structure of the page changes), you can construct an absolute XPath by listing all the elements from the root to the desired element.

Here’s an example of an absolute XPath expression:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumExample {
    public static void main(String[] args) {
        // Set the path of the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Create an instance of the ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Navigate to the Wikipedia Page
        driver.navigate().to("https://www.wikipedia.org/");

        WebElement element = driver.findElement(By.xpath("/html/body/div[3]/form/fieldset/button/i"));

        // Perform actions on the located element
       element.click()

        // Close the browser
        driver.quit();
    }
}

In the example above, /html/body/div[3]/form/fieldset/button/i is the absolute XPath expression. It specifies the complete path from the root (/html) to the desired i element.