Skip to content
CodeKruPro

XPath - Basics

13/70

XPath Basics

XPath is a powerful language for navigating XML/HTML documents. Key patterns:

  • //tag - find tag anywhere in document
  • //tag[@attr='value'] - find tag with attribute value
  • //tag[text()='value'] - find tag with exact text

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the button using XPath //button[@id='leftClick']
  3. Print its text
  4. Find the disabled button using XPath //button[@disabled]
  5. Print its text

Expected output:

Button 1: Left click on me
Button 2: Disabled button
Read full tutorial on Codekru

About this lesson

XPath basics with By.xpath in Selenium Java: tags, attributes and text

Learn Selenium JavaBeginnerCSS and XPath Locators

How it works

XPath describes a path through the page's element tree. //button means "a button element at any depth", and a predicate in square brackets filters it: [@id='leftClick'] tests an attribute value, [@disabled] only tests that the attribute exists, and [text()='...'] tests the element's own text.

Selenium hands the expression to the browser's XPath 1.0 engine through By.xpath(). The expression must select elements. Something like //button/@id selects attributes instead, and Selenium throws InvalidSelectorException, saying the result "should be an element".

Positions start at 1, not 0. (//button)[3] is the third button on the whole page, while //button[3] means "a button that is the third button child of its parent" and matches nothing on the Buttons page, because no parent there has three button children.

About the assignment

The Buttons page has eight buttons. The first one in the document is the sidebar toggle, whose getText() is an empty string. You will reach two specific buttons with XPath, one by its id and one by the presence of its disabled attribute, and read the label each shows.

What you will do

  1. Pass the attribute-match expression from the task to By.xpath() and find the element.
  2. Print its visible text with getText() after the label Button 1:.
  3. Find the second button with the expression that only checks that disabled is present. Only one button on the page has it.
  4. Print that button's text after the label Button 2:.

Common mistakes

  • Searching by tag alone

    //button with no predicate returns the first button in the document. Here that is the sidebar toggle, so getText() returns an empty string and the output looks blank.

  • Matching text that has spaces around it

    text()= compares the raw text, including surrounding whitespace. The two "Click me" buttons have a trailing space in their text, so an exact text() test finds 0 of them, while normalize-space() finds both.

  • Forgetting a bracket or quote

    An unclosed predicate is not valid XPath. Selenium throws InvalidSelectorException with "Unable to locate an element with the xpath expression".

In a real test suite

XPath earns its place when CSS cannot express the condition: matching on text, moving from a child up to its parent, or picking the element next to a known label. For plain id and class lookups, most teams still prefer CSS for speed and readability.

Lesson 12 showed that CSS cannot test text. The next lesson adds contains() and starts-with(), which match part of an attribute or part of the text.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        // TODO: Find button with XPath //button[@id='leftClick']
        // TODO: Print its text
        // TODO: Find disabled button with XPath //button[@disabled]
        // TODO: Print its text
        
        driver.quit();
    }
}
Read the full tutorial