What is the difference between relative and absolute XPath?

245    Asked by DeepakMistry in QA Testing , Asked on Jun 7, 2024

I am currently engaged in a particular task that is related to automating a test case for a web-based application that has dynamic content. The structure of the HTML can change, but the ID of the “submit” button should remain the same. In this particular scenario should I use the Xpath or a relative Xpath to locate the “submit” button? What is the difference between both in this particular scenario? 

Answered by Deepak Mistry

In the context of QA, in this particular scenario, you can use the relative Xpath to locate the “Submit” button. It is so because the relative Xpath expressions are preferred when you deal with dynamically web-based content as they are more resilient as compared to absolute Xpath in terms of changes in the HTML structure.

Here is an example given of how you can locate the “submit” button by using the relative XPath expression in the Python programming language with the selenium web driver:

From selenium import webdriver
# Initialize the WebDriver
Driver = webdriver.Chrome()
# Navigate to the web page
Driver.get(http://example.com)
# Find the “Submit” button using a relative XPath expression
Submit_button = driver.find_element_by_xpath(“//button[@id=’submit’]”)
# Click the “Submit” button
Submit_button.click()
# Close the WebDriver
Driver.quit()

In this particular example, the //button [@id=’Submit’] is a relative Xpath expression that can locate the button elements with the ID “Submit”. Even if the HTML structure had been changed, as long as the ID remains the same, this Xpath expression would successfully locate the “submit” button.

If you use the relative Xpath expression then it can help you in enhancing the maintainability and stability of the automation tests which would make them less prone to break when the web-based page structure changes.

Here is an example given of how you can locate the “submit” button by using a relative Xpath expression in Java programming language with the selenium WebDriver:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class RelativeXPathExample {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
        // Initialize the WebDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the web page
        Driver.get(http://example.com);
        // Find the “Submit” button using a relative XPath expression
        WebElement submitButton = driver.findElement(By.xpath(“//button[@id=’submit’]”));
        // Click the “Submit” button
        submitButton.click();
        // Close the WebDriver
        Driver.quit();
    }
}

Here is an example given of how you can locate the “submit” button by using a relative Xpath expression in python programming language with the selenium WebDriver:-

From selenium import webdriver
From selenium.webdriver.common.by import By
# Set the path to the ChromeDriver executable
Chrome_driver_path = “path/to/chromedriver”
# Initialize the WebDriver
Driver = webdriver.Chrome(chrome_driver_path)
# Navigate to the web page
Driver.get(http://example.com)
# Find the “Submit” button using a relative XPath expression
Submit_button_xpath = “//button[@id=’submit’]”
Submit_button = driver.find_element(By.XPATH, submit_button_xpath)
# Click the “Submit” button
Submit_button.click()
# Close the WebDriver
Driver.quit()


Your Answer

Interviews

Parent Categories