Skip to content
CodeKruPro

Find Elements with XPath

15/80

Find Elements with XPath

XPath can match a tag and an attribute. //button[@id='leftClick'] is the button with that id.

Task:

  1. Open Buttons
  2. Find the button with id leftClick
  3. Find the button that has a disabled attribute
  4. Print both texts

Print the lines below exactly.

Expected output:

Button 1: Left click on me
Button 2: Disabled button

About this lesson

Selenium Python By.XPATH basics: choose a button by tag and attribute

Selenium Python AssignmentsBeginnerCSS and XPath Locators

How it works

XPath describes a path through the page. A double slash at the start means "anywhere in the document", then comes a tag name (or a star for any tag) and conditions in square brackets. Inside the brackets an attribute is written with an at sign, so @id is the id attribute. A bare name without the at sign means a child element with that name, which is why a condition that forgets it matches nothing.

A condition on the bare attribute name, such as @disabled, is true when the attribute is present at all. That finds a button the page has switched off without needing its id, and this page has only one. Be careful with the start of the expression: button[...] or /button[...] raises NoSuchElementException, because button is not at the top of the document.

Selenium needs an expression that selects an element. One ending on an attribute raises InvalidSelectorException, because the result is an attribute node. A missing closing bracket raises the same exception. After the lookup, .text is a property, so you write it without parentheses. It returns the words a user sees on the button.

About the assignment

On the Buttons page, find two buttons using XPath only and print the visible text of each. The first is picked by a known id. The second is picked because it carries the disabled attribute.

What you will do

  1. Build an XPath that names the button tag and tests its id attribute against the id given in the task.
  2. Build a second XPath that names the button tag and requires the disabled attribute to exist.
  3. Pass each expression to find_element with the XPath strategy and read .text from each result.
  4. Print the two lines with the labels from the task.

Common mistakes

  • Leaving out the at sign

    A condition with an attribute name but no @ is treated as a child element. It matches nothing, and you get NoSuchElementException even though the attribute is on the page.

  • Starting without double slashes

    An expression that begins with only button looks at the top of the document and finds nothing. Start with a double slash.

  • Writing an unbalanced or attribute-ending expression

    A missing closing bracket raises InvalidSelectorException. So does an expression that ends on an attribute, because Selenium can only return elements.

In a real test suite

In a Python suite XPath is usually kept in a constants module or as class attributes of a page object. Teams prefer CSS when it can say the same thing, and keep XPath for text matching or walking up the tree.

The next lesson adds contains() and starts-with() so you can match part of a value.

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

Starter code

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

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Buttons")
    # Find both buttons with XPath and print their text
finally:
    driver.quit()