Bird
Raised Fist0
Selenium Javatesting~20 mins

Checkbox handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the isSelected() method do when working with checkboxes in Selenium Java?
easy
A. It unchecks the checkbox if it is selected.
B. It clicks the checkbox to select it.
C. It finds the checkbox element on the page.
D. It checks if the checkbox is currently selected or not.

Solution

  1. Step 1: Understand the purpose of isSelected()

    This method returns a boolean indicating if the checkbox is checked (true) or unchecked (false).
  2. Step 2: Differentiate from other actions

    Clicking changes state, finding locates elements, but isSelected() only checks state.
  3. Final Answer:

    It checks if the checkbox is currently selected or not. -> Option D
  4. Quick Check:

    Check state = isSelected() [OK]
Hint: Remember: isSelected() only checks checkbox state [OK]
Common Mistakes:
  • Confusing isSelected() with click()
  • Thinking isSelected() changes checkbox state
  • Using isSelected() to find elements
2. Which of the following is the correct way to click a checkbox located by its ID "subscribe" in Selenium Java?
easy
A. driver.click(By.id("subscribe"));
B. driver.findElement(By.name("subscribe")).click();
C. driver.findElement(By.id("subscribe")).click();
D. driver.findElement(By.id("subscribe")).select();

Solution

  1. Step 1: Identify correct method to find element by ID

    Use driver.findElement(By.id("subscribe")) to locate the checkbox by its ID.
  2. Step 2: Use correct method to click checkbox

    Use .click() on the found element to toggle checkbox state.
  3. Final Answer:

    driver.findElement(By.id("subscribe")).click(); -> Option C
  4. Quick Check:

    Find by ID + click() = driver.findElement(By.id("subscribe")).click(); [OK]
Hint: Use findElement(By.id()).click() to toggle checkbox [OK]
Common Mistakes:
  • Using driver.click() directly
  • Using wrong locator like By.name instead of By.id
  • Using non-existent select() method
3. Consider this code snippet:
WebElement checkbox = driver.findElement(By.id("agree"));
if (!checkbox.isSelected()) {
    checkbox.click();
}
System.out.println(checkbox.isSelected());
What will be printed if the checkbox was initially unchecked?
medium
A. false
B. true
C. null
D. Throws exception

Solution

  1. Step 1: Check initial checkbox state

    The checkbox is initially unchecked, so isSelected() returns false.
  2. Step 2: Click checkbox if not selected

    Since it is not selected, checkbox.click() runs, selecting it.
  3. Step 3: Print final state

    After clicking, isSelected() returns true, so true is printed.
  4. Final Answer:

    true -> Option B
  5. Quick Check:

    Unchecked + click = selected = true [OK]
Hint: Click only if not selected to ensure checkbox is checked [OK]
Common Mistakes:
  • Assuming initial state remains unchanged
  • Expecting false after click
  • Confusing isSelected() return values
4. You wrote this code to select a checkbox:
WebElement cb = driver.findElement(By.id("option1"));
if (cb.isSelected()) {
    cb.click();
}
But the checkbox remains unselected after running. What is the error?
medium
A. The condition should check if checkbox is NOT selected before clicking.
B. You should use cb.select() instead of cb.click().
C. The locator By.id("option1") is incorrect.
D. You need to use cb.isEnabled() before clicking.

Solution

  1. Step 1: Analyze the condition logic

    The code clicks the checkbox only if it is already selected, which unchecks it.
  2. Step 2: Understand desired action

    To select the checkbox, click it only if it is NOT selected.
  3. Step 3: Correct the condition

    Change condition to if (!cb.isSelected()) { cb.click(); } to select checkbox.
  4. Final Answer:

    The condition should check if checkbox is NOT selected before clicking. -> Option A
  5. Quick Check:

    Click only if not selected to select [OK]
Hint: Click checkbox only if it is not selected [OK]
Common Mistakes:
  • Clicking checkbox when already selected
  • Using non-existent select() method
  • Ignoring checkbox state before clicking
5. You want to ensure a checkbox with ID "terms" is selected before submitting a form. Which code snippet correctly handles this and avoids unnecessary clicks?
hard
A. WebElement cb = driver.findElement(By.id("terms")); if (!cb.isSelected()) { cb.click(); } // proceed to submit
B. WebElement cb = driver.findElement(By.id("terms")); cb.click(); // proceed to submit
C. WebElement cb = driver.findElement(By.id("terms")); if (cb.isSelected()) { cb.click(); } // proceed to submit
D. WebElement cb = driver.findElement(By.id("terms")); cb.isSelected(); // proceed to submit

Solution

  1. Step 1: Check checkbox state before clicking

    Use isSelected() to verify if checkbox is already selected.
  2. Step 2: Click only if not selected

    Click checkbox only when it is not selected to avoid toggling off.
  3. Step 3: Avoid unnecessary clicks

    WebElement cb = driver.findElement(By.id("terms")); if (!cb.isSelected()) { cb.click(); } // proceed to submit correctly implements this logic, ensuring checkbox is selected before submit.
  4. Final Answer:

    Code that clicks checkbox only if not selected before submit. -> Option A
  5. Quick Check:

    Check state, click if false, then submit [OK]
Hint: Click checkbox only if not selected to ensure correct state [OK]
Common Mistakes:
  • Clicking checkbox unconditionally causing toggle off
  • Clicking checkbox when already selected
  • Ignoring checkbox state before submit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard