About this lesson
Selenium Python XPath parent: step from a found element up with ..
Selenium Python AssignmentsBeginnerCSS and XPath Locators
How it works
Locators normally go down the page, from a container to the things inside it. XPath can also go up. Two dots mean "the parent of the current node", and parent::* says the same in longer form. This helps when the thing you can find easily is a small piece of text and the thing you need is the wrapper around it.
An element object in Selenium Python has its own find_element method. A relative path such as .. starts from that element, while one starting with // still searches the whole page. First find the nested strong tag, then ask that element for its parent. On this page the parent is a p tag with an id, which is why an id is there to read. Naming the wrong tag, such as parent::div, matches nothing.
Do not confuse this with element.parent. That attribute exists on every WebElement, but it returns the WebDriver object that made the element, not the parent in the HTML. Asking it for an id raises AttributeError. Read an id with get_attribute("id"), which returns a str.
About the assignment
On the Text Messages page a bold text sits inside a paragraph that has an id. You reach the bold text through that paragraph, print its text, then climb back up and print the id of the element you land on.
What you will do
- Find the nested
strongelement with an XPath that starts from the element with the given id and looks anywhere below it. - Print the text of the
strongelement. - Call
find_elementon that same element with an XPath that moves to the parent. - Read the
idattribute of the parent and print it after the label from the task.
Common mistakes
Using the element.parent attribute
It looks right, but it returns the driver object. Asking it for an attribute raises
AttributeError. Use an XPath that moves up instead.Searching below the strong tag
A path that looks for
strongbelow the found element raisesNoSuchElementException, because that tag has nostronginside it. Direction matters: going up needs two dots.Naming the wrong parent tag
Writing
parent::divwhen the parent is apraisesNoSuchElementException. The star form, or the two-dot shortcut, avoids guessing the tag.
In a real test suite
In real suites, walking up finds a row or a card from a label inside it, and then the test acts on the buttons in that same container. Teams wrap it in a helper like row_for(name), so tests never hold long paths.
You have now seen the main locator strategies, so the next lesson starts acting on what you find, beginning with a click.
Related lessons
- Find Elements with XPathThe XPath basics of tags and attributes that you need before climbing the tree.
- Locate a Heading with By.TAG_NAMEFinds a heading with
By.TAG_NAME, another way to reach an element by its tag. - Read Visible TextReads visible text with
.text, which is the first thing you print here.
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/TextMessages")
# Print the strong text, then the parent id
finally:
driver.quit()