Recall & Review
beginner
What does
findElement(By.xpath()) do in Selenium?It locates the first web element on the page that matches the given XPath expression.
Click to reveal answer
beginner
Write a simple XPath to find a button with the text 'Submit'.
XPath:
//button[text()='Submit'] finds a button element whose visible text is 'Submit'.Click to reveal answer
intermediate
Why is XPath useful compared to other locators like ID or class name?XPath can locate elements based on complex conditions like text, attributes, or hierarchy, even if IDs or classes are missing or dynamic.
Click to reveal answer
beginner
What happens if
findElement(By.xpath()) does not find any matching element?It throws a
NoSuchElementException, causing the test to fail unless handled.Click to reveal answer
beginner
How to write an XPath to find an input element with attribute
name='email'?XPath:
//input[@name='email'] selects an input element with the attribute name equal to 'email'.Click to reveal answer
What does
driver.findElement(By.xpath("//div[@id='main']")) do?✗ Incorrect
The XPath selects the first
element whose id attribute equals 'main'.
Which exception is thrown if
findElement(By.xpath()) finds no element?✗ Incorrect
If no matching element is found, Selenium throws NoSuchElementException.
Which XPath finds a button with text 'Login'?
✗ Incorrect
The XPath //button[text()='Login'] matches a button whose exact visible text is 'Login'.
What is the best practice for XPath locators?
✗ Incorrect
Relative XPath with unique attributes is more stable and easier to maintain.
Which XPath selects all <input> elements with type 'checkbox'?
✗ Incorrect
XPath //input[@type='checkbox'] selects input elements with attribute type equal to 'checkbox'.
Explain how to use
findElement(By.xpath()) to locate a web element and what happens if the element is not found.Think about how Selenium searches the page and error handling.
You got /3 concepts.
Describe best practices for writing XPath expressions for Selenium tests.
Consider what makes locators reliable and easy to update.
You got /4 concepts.