Which statement best describes an absolute XPath in Selenium?
AIt uses CSS selectors to locate elements.
BIt starts with '//' and searches anywhere in the document.
CIt starts from the root node and specifies the full path to the element.
DIt only works with elements having unique IDs.
Attempts:
2 left
💡 Hint
Think about how the path starts from the very top of the HTML document.
✗ Incorrect
Absolute XPath starts from the root node (html) and follows the full path down to the target element, making it very specific but fragile if the page structure changes.
❓ Predict Output
intermediate
1:30remaining
Output of XPath Locator in Selenium
Given the following HTML snippet and XPath, what element text will Selenium find?
The XPath '/html/body/div/p[2]' selects the second
element inside the
, which contains the text 'Second'.
❓ locator
advanced
2:00remaining
Choosing the Correct Relative XPath
Which XPath expression correctly selects all elements with a class attribute containing 'search' anywhere in the document?
A/input[contains(@class, 'search')]
B/html/body//input[contains(@class, 'search')]
Cinput[contains(@class, 'search')]
D//input[contains(@class, 'search')]
Attempts:
2 left
💡 Hint
Relative XPath starts with '//' to search anywhere in the document.
✗ Incorrect
Option D uses relative XPath starting with '//' to find all elements anywhere with class containing 'search'. Option D is absolute but unnecessarily long. Option D is invalid because it starts with a single slash but no root node. Option D is invalid XPath syntax.
❓ assertion
advanced
1:30remaining
Validating Element Presence Using XPath
Which assertion correctly verifies that an element located by relative XPath exists on the page using Selenium in Python?
Selenium Python
element = driver.find_element(By.XPATH, "//button[@id='submit']")
Aassert element is not None
Bassert element.exists()
Cassert driver.find_element(By.XPATH, "//button[@id='submit']") is True
find_element throws an exception if not found, so checking for None is safe after catching exceptions.
✗ Incorrect
Option A correctly asserts that the element variable is not None, meaning the element was found. Options A and C compare the element object to True, which is invalid. Option A calls a non-existent method 'exists()'.
🔧 Debug
expert
2:30remaining
Debugging a Failing XPath Locator
You have this XPath locator in your Selenium test: '/html/body/div[2]/span/input'. The test fails with NoSuchElementException. Which is the most likely reason?
AThe page structure changed, so the absolute XPath no longer matches the element.
BRelative XPath should be used instead of absolute XPath for all elements.
CThe input element does not have an ID attribute.
DThe XPath syntax is invalid because it uses numbers in brackets.
Attempts:
2 left
💡 Hint
Absolute XPath is fragile and breaks if page layout changes.
✗ Incorrect
Absolute XPath depends on exact page structure. If the page changes, the path may no longer point to the element, causing NoSuchElementException. The lack of ID does not cause this error. Using relative XPath is a good practice but not the cause of failure. The XPath syntax with numbers in brackets is valid.
Practice
(1/5)
1. Which of the following best describes an absolute XPath?
easy
A. It starts from the root node and follows the full path to the element.
B. It starts from any node and uses conditions to find the element.
C. It uses CSS selectors instead of XML paths.
D. It only works with HTML tables.
Solution
Step 1: Understand XPath types
Absolute XPath always starts from the root node, indicated by a single slash (/), and follows the full path to the target element.
Step 2: Compare with relative XPath
Relative XPath starts from anywhere in the document, often using double slashes (//) and conditions to locate elements.
Final Answer:
It starts from the root node and follows the full path to the element. -> Option A
Quick Check:
Absolute XPath = full path from root [OK]
Hint: Absolute XPath always starts with a single slash / [OK]
Common Mistakes:
Confusing absolute with relative XPath syntax
Thinking relative XPath always starts at root
Assuming absolute XPath uses conditions only
2. Which of the following is a correct syntax for a relative XPath to find a button with text 'Submit'?
easy
A. /html/body/button[text()='Submit']
B. //button[text()='Submit']
C. button[text()='Submit']
D. //*[@id='submit']
Solution
Step 1: Identify relative XPath syntax
Relative XPath uses double slashes (//) to search anywhere in the document for matching elements.
Step 2: Check the options
//button[text()='Submit'] uses //button[text()='Submit'], which correctly finds a button element with exact text 'Submit'. /html/body/button[text()='Submit'] is absolute XPath. button[text()='Submit'] misses the // and is invalid XPath. //*[@id='submit'] uses an attribute selector but does not specify text.
Final Answer:
//button[text()='Submit'] -> Option B
Quick Check:
Relative XPath uses // and conditions [OK]
Hint: Relative XPath starts with // and uses conditions [OK]
child element of any <ul> element anywhere in the document.
Step 2: Match with HTML structure
The <ul> has three
children: Home (1), About (2), Contact (3). So li[2] selects About.
Final Answer:
The second list item <li>About</li> -> Option A
Quick Check:
li[2] = second child element [OK]
Hint: XPath [n] selects the nth child element [OK]
Common Mistakes:
Confusing index starting at 0 instead of 1
Thinking //ul/li[2] selects second ul element
Assuming XPath is zero-based
4. You wrote this XPath to find a login button: /html/body/div/button[@id='login'] But your test fails to find the element after page update. What is the likely issue?
medium
A. XPath cannot find buttons.
B. The XPath uses wrong attribute syntax.
C. The button does not have an id attribute.
D. The XPath is absolute and fragile; page structure changed breaking the path.
Solution
Step 1: Analyze the XPath type
The XPath starts with a single slash /, so it is absolute and depends on the full path from root.
Step 2: Understand fragility of absolute XPath
If the page structure changes (like adding a div), the absolute path breaks and element is not found.
Final Answer:
The XPath is absolute and fragile; page structure changed breaking the path. -> Option D
Quick Check:
Absolute XPath breaks if page structure changes [OK]
Hint: Avoid absolute XPath for dynamic pages [OK]
Common Mistakes:
Blaming attribute syntax instead of XPath type
Assuming XPath cannot find buttons
Ignoring page structure changes
5. You want to write a stable XPath to find a product name inside a list where the product has a price of '$20'. The HTML structure is:
Which XPath is best to find the name of the product priced $20?
hard
A. //span[@class='price' and text()='$20']/preceding-sibling::span[@class='name']
B. /html/body/div[1]/span[1]
C. //div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name']
D. //div[@class='product']/span[@class='name']
Solution
Step 1: Understand the requirement
We want the product name where the price is exactly '$20'. So we must find a product div with a price span of '$20' and then get its name span.
Step 2: Evaluate each XPath
//div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name'] selects div with class 'product' having a span price '$20', then selects the name span inside it. This is precise and stable. /html/body/div[1]/span[1] is absolute XPath and fragile. //span[@class='price' and text()='$20']/preceding-sibling::span[@class='name'] tries to find price span then preceding sibling name span, which matches the HTML structure where name is before price. //div[@class='product']/span[@class='name'] selects all product names, ignoring price.
Final Answer:
//div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name'] -> Option C
Quick Check:
Use relative XPath with conditions for stable element [OK]
Hint: Use relative XPath with conditions to target elements precisely [OK]
Common Mistakes:
Using absolute XPath that breaks easily
Selecting elements without conditions
Ignoring element hierarchy in XPath
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.