0
0
Selenium-pythonHow-ToBeginner ยท 4 min read

How to Write XPath in Selenium: Syntax, Examples, and Tips

In Selenium, you write xpath expressions to locate elements on a web page using the By.xpath() method. XPath uses a path-like syntax to navigate through elements and attributes in the HTML document.
๐Ÿ“

Syntax

XPath expressions use a path syntax to find elements. The basic parts include:

  • //tagname: Selects all elements with the given tag name anywhere in the document.
  • [@attribute='value']: Filters elements by attribute value.
  • /: Selects direct child elements.
  • text(): Matches element text content.

Combine these to create precise locators.

java
By.xpath("//tagname[@attribute='value']")
๐Ÿ’ป

Example

This example shows how to open a browser, navigate to a page, and find a button by XPath using Selenium WebDriver in Java.

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

public class XPathExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");

        // Locate button with text 'Submit'
        WebElement submitButton = driver.findElement(By.xpath("//button[text()='Submit']"));

        // Click the button
        submitButton.click();

        System.out.println("Button clicked successfully.");
        driver.quit();
    }
}
Output
Button clicked successfully.
โš ๏ธ

Common Pitfalls

Common mistakes when writing XPath in Selenium include:

  • Using absolute XPath (starting with /html) which breaks easily if page structure changes.
  • Not escaping quotes properly inside XPath strings.
  • Using overly complex XPath expressions that are hard to read and maintain.
  • Ignoring case sensitivity in tag names or attribute values.

Prefer relative XPath starting with // and use attributes or text for stable locators.

java
/* Wrong: Absolute XPath (fragile) */
By.xpath("/html/body/div[2]/div/button")

/* Right: Relative XPath with attribute */
By.xpath("//button[@id='submitBtn']")
๐Ÿ“Š

Quick Reference

XPath ExpressionDescription
//tagnameSelects all elements with the tag name anywhere
//tagname[@attr='value']Selects elements with attribute equal to value
//tagname[contains(@attr, 'value')]Selects elements where attribute contains value
//tagname[text()='text']Selects elements with exact text content
//tagname[starts-with(@attr, 'value')]Selects elements where attribute starts with value
(//tagname)[index]Selects the element at the given index (1-based)
โœ…

Key Takeaways

Use By.xpath() with relative XPath expressions for stable element location.
Avoid absolute XPath as it breaks easily with page changes.
Use attributes and text() in XPath to create precise locators.
Escape quotes properly inside XPath strings in code.
Test XPath expressions in browser DevTools before using in Selenium.