Skip to content
CodeKruPro

Move to a Parent with XPath

17/80

Move to a Parent with XPath

From an element you already found, .. is its parent.

Task:

  1. Find the strong inside #boldText
  2. Print the strong text
  3. Move to the parent and print its id

Print the lines below exactly.

Expected output:

Strong text: A Bold Text
Parent ID: boldText

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

  1. Find the nested strong element with an XPath that starts from the element with the given id and looks anywhere below it.
  2. Print the text of the strong element.
  3. Call find_element on that same element with an XPath that moves to the parent.
  4. Read the id attribute 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 strong below the found element raises NoSuchElementException, because that tag has no strong inside it. Direction matters: going up needs two dots.

  • Naming the wrong parent tag

    Writing parent::div when the parent is a p raises NoSuchElementException. 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.

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/TextMessages")
    # Print the strong text, then the parent id
finally:
    driver.quit()