Challenge - 5 Problems
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of findElement with XPath using contains()
What will be the output of the following Selenium Java code snippet when the page contains a button with text 'Submit Form'?
Selenium Java
WebElement button = driver.findElement(By.xpath("//button[contains(text(),'Submit')]");
System.out.println(button.getText());Attempts:
2 left
💡 Hint
Remember that contains() matches any substring inside the text node.
✗ Incorrect
The XPath expression selects the button element whose text contains 'Submit'. The getText() method returns the full visible text of the element, which is 'Submit Form'.
❓ assertion
intermediate2:00remaining
Correct assertion for element found by XPath
Which assertion correctly verifies that an element found by XPath has the expected text 'Login'?
Selenium Java
WebElement loginButton = driver.findElement(By.xpath("//button[@id='login']"));Attempts:
2 left
💡 Hint
Use the correct assertion method to compare strings in Java.
✗ Incorrect
assertEquals compares the expected and actual strings correctly. Using '==' compares references, which is incorrect for strings.
❓ locator
advanced2:00remaining
Best XPath locator for a button with dynamic id but fixed text
Given a button with a dynamic id but fixed visible text 'Confirm', which XPath locator is best to find it reliably?
Attempts:
2 left
💡 Hint
Focus on stable attributes or text for locating elements.
✗ Incorrect
Option A uses exact text match which is stable. Option A depends on partial id which is dynamic. Option A uses fixed id which changes. Option A uses partial text which may match multiple elements.
🔧 Debug
advanced2:00remaining
Identify the error in XPath usage
What error will occur when running this Selenium Java code?
WebElement element = driver.findElement(By.xpath("//div[@class='content']"));
String text = element.getText();
System.out.println(text);
Selenium Java
WebElement element = driver.findElement(By.xpath("//div[@class='content']"));
String text = element.getText();
System.out.println(text);Attempts:
2 left
💡 Hint
Consider what happens if the element is not found.
✗ Incorrect
If the element is not found, findElement throws NoSuchElementException. It does not return null, so NullPointerException won't occur here.
❓ framework
expert2:00remaining
Best practice for waiting for element by XPath before interaction
In Selenium Java, which approach correctly waits up to 10 seconds for an element located by XPath before clicking it?
Attempts:
2 left
💡 Hint
Use explicit waits for better reliability than implicit waits or sleep.
✗ Incorrect
Option C uses explicit wait with ExpectedConditions which waits until the element is clickable before clicking. Option C uses fixed sleep which is inefficient. Option C does not wait. Option C sets implicit wait but explicit wait is preferred for specific conditions.