Challenge - 5 Problems
FindElement by ID Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that tries to find an element by ID and print its text. What will be printed if the element with ID "submitBtn" contains the text "Submit"?
Selenium Java
WebElement button = driver.findElement(By.id("submitBtn"));
System.out.println(button.getText());Attempts:
2 left
💡 Hint
The getText() method returns the visible text of the element.
✗ Incorrect
The findElement(By.id("submitBtn")) locates the element with ID 'submitBtn'. Calling getText() on this element returns its visible text, which is 'Submit'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the text of an element found by ID?
You want to verify that the element with ID "welcomeMsg" contains the text "Welcome User". Which assertion is correct in JUnit?
Selenium Java
WebElement message = driver.findElement(By.id("welcomeMsg"));Attempts:
2 left
💡 Hint
Use assertEquals to compare strings in JUnit.
✗ Incorrect
assertEquals(expected, actual) correctly compares the expected string with the actual text. Using '==' compares references, which is incorrect for strings.
❓ locator
advanced2:00remaining
Which locator correctly finds an element by ID in Selenium Java?
You want to find an element with the ID "loginInput". Which locator code is correct?
Attempts:
2 left
💡 Hint
ID is unique and located with By.id.
✗ Incorrect
By.id("loginInput") finds the element with the exact ID. Other locators search by different attributes or tags.
🔧 Debug
advanced2:00remaining
What error occurs if findElement(By.id) does not find any element?
If the element with ID "nonExistent" is not present on the page, what happens when this code runs?
WebElement elem = driver.findElement(By.id("nonExistent"));
Selenium Java
WebElement elem = driver.findElement(By.id("nonExistent"));Attempts:
2 left
💡 Hint
findElement throws an exception if no element is found.
✗ Incorrect
findElement throws NoSuchElementException immediately if the element is not found. It does not return null.
❓ framework
expert3:00remaining
In a Selenium Java test framework, which method best waits for an element by ID before interacting?
You want to wait up to 10 seconds for an element with ID "submitBtn" to be clickable before clicking it. Which code snippet correctly uses WebDriverWait?
Attempts:
2 left
💡 Hint
Explicit waits wait for specific conditions before proceeding.
✗ Incorrect
Option D uses explicit wait with WebDriverWait and ExpectedConditions to wait until the element is clickable, then clicks it. Option D clicks immediately and may fail if element not ready. Option D uses fixed sleep which is inefficient. Option D sets implicit wait but does not wait for clickability.