XPath is a language used to locate elements on a web page by navigating through the HTML structure.
Click to reveal answer
beginner
How do you find an element by XPath in Selenium with Python?
Use driver.find_element(By.XPATH, 'your_xpath_here') to locate the element.
Click to reveal answer
intermediate
What is the difference between absolute and relative XPath?
Absolute XPath starts from the root element and follows the full path, while relative XPath starts from anywhere in the document and is shorter and more flexible.
Click to reveal answer
intermediate
Why should you prefer relative XPath over absolute XPath?
Relative XPath is less fragile and more maintainable because it does not depend on the full path, which can change often.
Click to reveal answer
beginner
Give an example of a valid XPath to find a button with text 'Submit'.
Example: //button[text()='Submit']
Click to reveal answer
Which Selenium method is used to find an element by XPath in Python?
Adriver.find_element_by_class_name('class')
Bdriver.find_element_by_id('id')
Cdriver.find_element(By.XPATH, 'xpath')
Ddriver.find_element_by_name('name')
✗ Incorrect
The correct method to find an element by XPath is driver.find_element(By.XPATH, 'xpath').
What does the XPath expression //div[@class='header'] select?
AAll elements with id 'header'
BThe first div element
CAll elements named 'header'
DAll div elements with class 'header'
✗ Incorrect
The expression selects all div elements that have a class attribute equal to 'header'.
Which XPath is an example of absolute XPath?
A/html/body/div[1]/div[2]/button
B//button[@id='submit']
C//div[@class='content']//a
D//input[@name='email']
✗ Incorrect
Absolute XPath starts from the root and follows the full path, like /html/body/div[1]/div[2]/button.
Why is relative XPath preferred over absolute XPath?
AIt is longer and more detailed
BIt is shorter and less likely to break if page structure changes
CIt always selects multiple elements
DIt requires no attributes
✗ Incorrect
Relative XPath is shorter and more flexible, so it is less likely to break when the page structure changes.
What does the XPath //input[@type='checkbox'] select?
AAll input elements with type attribute equal to 'checkbox'
BThe first input element
CAll elements with class 'checkbox'
DAll checkbox elements
✗ Incorrect
It selects all input elements where the type attribute is 'checkbox'.
Explain how to find an element by XPath in Selenium using Python.
Think about the Selenium method and locator type.
You got /3 concepts.
Describe the difference between absolute and relative XPath and why one is preferred.
Consider how page changes affect XPath.
You got /3 concepts.
Practice
(1/5)
1. What does the following Selenium Python code do? driver.find_element(By.XPATH, "//button[@id='submit']")
easy
A. Finds the button element by its class name 'submit'
B. Finds all button elements with id 'submit' on the page
C. Clicks the button with id 'submit'
D. Finds the first button element with id 'submit' on the page
Solution
Step 1: Understand the method used
The method find_element returns the first matching element found by the locator.
Step 2: Analyze the XPath expression
The XPath //button[@id='submit'] selects button elements with attribute id='submit'.
Final Answer:
Finds the first button element with id 'submit' on the page -> Option D
Quick Check:
find_element + XPath with id attribute = first matching element [OK]
Hint: find_element finds only the first matching element [OK]
Common Mistakes:
Confusing find_element with find_elements
Thinking XPath finds by class instead of id
Assuming it clicks the element automatically
2. Which of the following is the correct syntax to find an element by XPath in Selenium Python?
easy
A. driver.find_element(By.XPATH, "//div[@class='header']")
B. driver.find_element_by_xpath("//div[@class='header']")
C. driver.find_element(By.CSS_SELECTOR, "//div[@class='header']")
D. driver.find_element(By.ID, "//div[@class='header']")
Solution
Step 1: Recall the modern Selenium syntax
Since Selenium 4, the recommended way is driver.find_element(By.XPATH, xpath_string).
Step 2: Check each option
driver.find_element(By.XPATH, "//div[@class='header']") uses the correct syntax. driver.find_element_by_xpath("//div[@class='header']") is deprecated. Options C and D use wrong locator types for XPath.
Final Answer:
driver.find_element(By.XPATH, "//div[@class='header']") -> Option A
Quick Check:
Use By.XPATH with find_element [OK]
Hint: Use By.XPATH with find_element, not deprecated methods [OK]
Common Mistakes:
Using deprecated find_element_by_xpath method
Mixing locator types like CSS_SELECTOR with XPath
Passing XPath as ID locator
3. What will be the output of this code snippet if the page contains exactly one <input name='search'> element?
element = driver.find_element(By.XPATH, "//input[@name='search']")
print(element.tag_name)
medium
A. input
B. search
C. NoSuchElementException
D. button
Solution
Step 1: Understand what find_element returns
It returns the first matching WebElement for the XPath.
Step 2: Check the tag_name property
The element found is an input tag, so element.tag_name returns "input".
Hint: tag_name returns the HTML tag of the found element [OK]
Common Mistakes:
Confusing attribute value with tag name
Expecting exception when element exists
Assuming tag_name returns attribute value
4. What is wrong with this code snippet?
element = driver.find_element(By.XPATH, "//div[@class='content']")
print(element.text)
Assume the page has multiple <div class='content'> elements.
medium
A. It will raise NoSuchElementException
B. It only finds the first matching element, ignoring others
C. XPath syntax is incorrect
D. element.text will return None
Solution
Step 1: Understand find_element behavior
find_element returns only the first matching element, even if multiple exist.
Step 2: Analyze the impact
If multiple div.content exist, only the first is found and printed, others ignored.
Final Answer:
It only finds the first matching element, ignoring others -> Option B
Quick Check:
find_element returns first match only [OK]
Hint: Use find_elements to get all matches, find_element gets first only [OK]
Common Mistakes:
Expecting find_element to return all matches
Thinking XPath syntax is wrong
Assuming element.text returns None if multiple elements exist
5. You want to find a button element with text exactly 'Submit' using XPath in Selenium Python. Which XPath expression is correct to use with driver.find_element(By.XPATH, xpath)?
hard
A. //button[text()!='Submit']
B. //button[contains(text(), 'Submit')]
C. //button[text()='Submit']
D. //button[@text='Submit']
Solution
Step 1: Understand XPath text() function
XPath text()='Submit' matches elements whose text content is exactly 'Submit'.
Step 2: Evaluate each option
//button[text()='Submit'] matches exact text. //button[contains(text(), 'Submit')] matches partial text (not exact). //button[@text='Submit'] is invalid because text is not an attribute. //button[text()!='Submit'] matches elements whose text is not 'Submit'.
Final Answer:
//button[text()='Submit'] -> Option C
Quick Check:
Use text()='value' for exact text match in XPath [OK]
Hint: Use text()='value' for exact text match in XPath [OK]
Common Mistakes:
Using @text instead of text() function
Using contains() when exact match needed
Using != in XPath for positive match
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.