How can I use the XPath expression for locating and interacting with specific elements on the web page?

254    Asked by CarolynBuckland in QA Testing , Asked on Apr 5, 2024

I am being asked about the automation of tests for a web-based application that has dynamic elements and a complex nested structure. Describe to me how can I use the XPath expression effectively for locating and interacting with specific elements on the web page. 

Answered by Carolyn Buckland

In the context of selenium, here are the steps given:-

Use absolute XPath

You can begin from the root of the HTML document and provide the full path to the element. However, you can also start from Relative XPath from a specific node in an HTML document.

Avoid busing index in XPath

You can use the indexes in Xpath which would make them brittle and prone to breaking of the order of elements changes.

Use attribute and text content

You can use the various attributes to create a more reliable Xpath expression.

Handling dynamic elements

You can use the Xpath function like contain () when dealing with dynamic elements that have changing attributes or values.

Avoid lengthy XPath expression

You should use the short Xpath expression as the lengthy Xpath expression can make it difficult to understand.

Here is the example given of how you can us e the Xpath for locating bands interacting with these elements:-

From selenium import webdriver

# Initialize WebDriver (e.g., Chrome)
Driver = webdriver.Chrome()
# Navigate to the login page
Driver.get(https://example.com/login)
# Find username and password input fields using XPath
Username_input = driver.find_element_by_xpath(“//input[@id=’username’]”)
Password_input = driver.find_element_by_xpath(“//input[@id=’password’]”)
# Enter username and password
Username_input.send_keys(“your_username”)
Password_input.send_keys(“your_password”)
# Find and click the login button using XPath
Login_button = driver.find_element_by_xpath(“//button[contains(text(), ‘Login’)]”)
Login_button.click()
# Continue with your testing or verification steps after login
Here is the example given in java programming language for 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 LoginAutomation {
    Public static void main(String[] args) {
        // Set the path to the Chrome driver executable (Download from https://chromedriver.chromium.org/downloads)
        System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
        // Initialize WebDriver (e.g., Chrome)
        WebDriver driver = new ChromeDriver();
        // Navigate to the login page
        Driver.get(https://example.com/login);
        // Find username and password input fields using XPath
        WebElement usernameInput = driver.findElement(By.xpath(“//input[@id=’username’]”));
        WebElement passwordInput = driver.findElement(By.xpath(“//input[@id=’password’]”));
        // Enter username and password
        usernameInput.sendKeys(“your_username”);
        passwordInput.sendKeys(“your_password”);
        // Find and click the login button using XPath
        WebElement loginButton = driver.findElement(By.xpath(“//button[contains(text(), ‘Login’)]”));
        loginButton.click();
        // Continue with your testing or verification steps after login
    }
}


Your Answer

Interviews

Parent Categories