0
0
Selenium Javatesting~20 mins

findElement by xpath in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
ASubmit
BEmpty string
CNoSuchElementException
DSubmit Form
Attempts:
2 left
💡 Hint
Remember that contains() matches any substring inside the text node.
assertion
intermediate
2: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']"));
AassertEquals("Login", loginButton.getText());
BassertTrue(loginButton.getText() == "Login");
CassertFalse(loginButton.getText().equals("Login"));
DassertNull(loginButton.getText());
Attempts:
2 left
💡 Hint
Use the correct assertion method to compare strings in Java.
locator
advanced
2: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?
A//button[text()='Confirm']
B//button[@id='btn123']
C//button[contains(@id, 'btn') and text()='Confirm']
D//button[contains(text(), 'Conf')]
Attempts:
2 left
💡 Hint
Focus on stable attributes or text for locating elements.
🔧 Debug
advanced
2: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);
ANullPointerException because element is null
BNoSuchElementException if no div with class 'content' exists
CSyntaxError in XPath expression
DClassCastException when casting WebElement
Attempts:
2 left
💡 Hint
Consider what happens if the element is not found.
framework
expert
2: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?
AThread.sleep(10000); driver.findElement(By.xpath("//button[@id='submit']")).click();
Bdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.xpath("//button[@id='submit']")).click();
Cnew WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='submit']"))).click();
Ddriver.findElement(By.xpath("//button[@id='submit']")).click();
Attempts:
2 left
💡 Hint
Use explicit waits for better reliability than implicit waits or sleep.