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
- Build an XPath that names the
buttontag and tests itsidattribute against the id given in the task. - Build a second XPath that names the
buttontag and requires thedisabledattribute to exist. - Pass each expression to
find_elementwith the XPath strategy and read.textfrom each result. - 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 getNoSuchElementExceptioneven though the attribute is on the page.Starting without double slashes
An expression that begins with only
buttonlooks 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.
Related lessons
- Find Elements with CSSThe CSS way to pick an element by id and tag, for comparison with XPath.
- Use contains and starts-with in XPathAdds partial matching on text and attributes to the XPath you learn here.
- Read Visible TextCovers
.textin more depth, including what counts as visible text.
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.
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()