0
0
Selenium Javatesting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FindElement by ID Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
ASubmit
BsubmitBtn
Cnull
DNoSuchElementException
Attempts:
2 left
💡 Hint
The getText() method returns the visible text of the element.
assertion
intermediate
2: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"));
AassertFalse(message.getText().equals("Welcome User"));
BassertTrue(message.getText() == "Welcome User");
CassertEquals("Welcome User", message.getText());
DassertNull(message.getText());
Attempts:
2 left
💡 Hint
Use assertEquals to compare strings in JUnit.
locator
advanced
2: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?
Adriver.findElement(By.id("loginInput"));
Bdriver.findElement(By.name("loginInput"));
Cdriver.findElement(By.className("loginInput"));
Ddriver.findElement(By.tagName("loginInput"));
Attempts:
2 left
💡 Hint
ID is unique and located with By.id.
🔧 Debug
advanced
2: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"));
ANullPointerException
BNoSuchElementException
CTimeoutException
DElementNotVisibleException
Attempts:
2 left
💡 Hint
findElement throws an exception if no element is found.
framework
expert
3: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?
Adriver.findElement(By.id("submitBtn")).click();
Bdriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.findElement(By.id("submitBtn")).click();
CThread.sleep(10000); driver.findElement(By.id("submitBtn")).click();
Dnew WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.id("submitBtn"))).click();
Attempts:
2 left
💡 Hint
Explicit waits wait for specific conditions before proceeding.