0
0
Selenium Javatesting~20 mins

Checkbox handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Checkbox Mastery
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 checks a checkbox and prints its status. What will be printed?

Selenium Java
WebElement checkbox = driver.findElement(By.id("subscribe"));
checkbox.click();
System.out.println(checkbox.isSelected());
Atrue
Bfalse
CNoSuchElementException
DElementNotInteractableException
Attempts:
2 left
💡 Hint

Clicking a checkbox toggles its selection state.

assertion
intermediate
2:00remaining
Which assertion correctly verifies a checkbox is unchecked?

You want to assert that a checkbox with id "terms" is not selected. Which assertion is correct?

AassertFalse(driver.findElement(By.id("terms")).isSelected());
BassertTrue(driver.findElement(By.id("terms")).isSelected());
CassertEquals(true, driver.findElement(By.id("terms")).isSelected());
DassertEquals("unchecked", driver.findElement(By.id("terms")).getAttribute("checked"));
Attempts:
2 left
💡 Hint

Use assertFalse to check something is false.

locator
advanced
2:00remaining
Which locator is best to find a checkbox with label text "Accept"?

You want to locate a checkbox input that has a label with text "Accept". Which locator is best practice?

ABy.xpath("//label[text()='Accept']/input")
BBy.id("Accept")
CBy.cssSelector("input[type='checkbox'][label='Accept']")
DBy.xpath("//input[@type='checkbox' and @id=//label[text()='Accept']/@for]")
Attempts:
2 left
💡 Hint

Labels often use the for attribute to link to checkbox id.

🔧 Debug
advanced
2:00remaining
Why does this checkbox click fail with ElementNotInteractableException?

Given this code snippet, clicking the checkbox throws ElementNotInteractableException. Why?

WebElement checkbox = driver.findElement(By.id("newsletter"));
checkbox.click();
AThe checkbox id is incorrect, so element is not found.
BThe checkbox is hidden or covered by another element, so it cannot be clicked.
CThe checkbox is already selected, so click is not allowed.
DThe driver is not initialized properly.
Attempts:
2 left
💡 Hint

ElementNotInteractableException means the element is present but not clickable.

framework
expert
2:00remaining
In a test framework, how to best verify multiple checkboxes are all selected?

You have a list of checkbox elements. Which code snippet correctly asserts all are selected?

AassertEquals(checkboxes.size(), checkboxes.stream().filter(cb -> cb.isSelected()).count());
Bfor(WebElement cb : checkboxes) { assertTrue(cb.isSelected()); }
CassertTrue(checkboxes.stream().allMatch(cb -> cb.isSelected()));
DassertFalse(checkboxes.stream().anyMatch(cb -> !cb.isSelected()));
Attempts:
2 left
💡 Hint

Use Java streams to check all elements satisfy a condition.