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
- Pass the attribute-match expression from the task to
By.xpath()and find the element. - Print its visible text with
getText()after the labelButton 1:. - Find the second button with the expression that only checks that
disabledis present. Only one button on the page has it. - Print that button's text after the label
Button 2:.
Common mistakes
Searching by tag alone
//buttonwith no predicate returns the first button in the document. Here that is the sidebar toggle, sogetText()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 exacttext()test finds 0 of them, whilenormalize-space()finds both.Forgetting a bracket or quote
An unclosed predicate is not valid XPath. Selenium throws
InvalidSelectorExceptionwith "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.
Related lessons
- XPath Functions - contains() and starts-with()Partial matching on text and attributes with XPath functions.
- XPath Axes - Parent and SiblingMove up and sideways through the tree with XPath axes.
- CSS Selectors - BasicsThe CSS way to write the same id and tag lookups.
- Click a ButtonClick the button you just located and watch its text change.
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.
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();
}
}