Bird
Raised Fist0
Selenium Pythontesting~15 mins

Checking element state (displayed, enabled, selected) in Selenium Python - Build an Automation Script

Choose your learning style10 modes available
Verify element states on the sample form page
Preconditions (2)
Step 1: Open the sample form page URL in the browser
Step 2: Locate the checkbox with id 'subscribe-newsletter'
Step 3: Verify the checkbox is displayed on the page
Step 4: Verify the checkbox is enabled (clickable)
Step 5: Verify the checkbox is not selected by default
Step 6: Click the checkbox to select it
Step 7: Verify the checkbox is now selected
Step 8: Locate the submit button with id 'submit-btn'
Step 9: Verify the submit button is displayed and enabled
✅ Expected Result: Checkbox is visible, enabled, initially not selected, becomes selected after clicking. Submit button is visible and enabled.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert checkbox is displayed
Assert checkbox is enabled
Assert checkbox is not selected initially
Assert checkbox is selected after clicking
Assert submit button is displayed
Assert submit button is enabled
Best Practices:
Use explicit waits to wait for elements to be present and interactable
Use By.ID locator strategy for stable element identification
Use assert statements for validation
Structure code with setup and teardown for browser management
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestElementStates(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/sample-form')
        self.wait = WebDriverWait(self.driver, 10)

    def test_checkbox_and_button_states(self):
        driver = self.driver
        wait = self.wait

        # Wait for checkbox to be visible
        checkbox = wait.until(EC.visibility_of_element_located((By.ID, 'subscribe-newsletter')))

        # Assert checkbox is displayed
        self.assertTrue(checkbox.is_displayed(), 'Checkbox should be displayed')

        # Assert checkbox is enabled
        self.assertTrue(checkbox.is_enabled(), 'Checkbox should be enabled')

        # Assert checkbox is not selected initially
        self.assertFalse(checkbox.is_selected(), 'Checkbox should not be selected initially')

        # Click checkbox to select it
        checkbox.click()

        # Assert checkbox is selected after clicking
        self.assertTrue(checkbox.is_selected(), 'Checkbox should be selected after clicking')

        # Wait for submit button to be visible and enabled
        submit_btn = wait.until(EC.element_to_be_clickable((By.ID, 'submit-btn')))

        # Assert submit button is displayed
        self.assertTrue(submit_btn.is_displayed(), 'Submit button should be displayed')

        # Assert submit button is enabled
        self.assertTrue(submit_btn.is_enabled(), 'Submit button should be enabled')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This test script uses Python's unittest framework with Selenium WebDriver.

setUp() opens the browser and navigates to the sample form page.

We use WebDriverWait with expected_conditions to wait until elements are visible or clickable before interacting.

We locate the checkbox by its ID and check if it is displayed, enabled, and not selected initially using is_displayed(), is_enabled(), and is_selected() methods.

Then we click the checkbox and verify it becomes selected.

Similarly, we locate the submit button and verify it is displayed and enabled.

tearDown() closes the browser after the test.

This structure ensures reliable and readable tests following best practices.

Common Mistakes - 4 Pitfalls
Using hardcoded sleep (time.sleep) instead of explicit waits
Using XPath with absolute paths for locating elements
Not checking if element is displayed before interacting
Not quitting the browser after test
Bonus Challenge

Now add data-driven testing to check checkbox selection state with 3 different initial states (selected, not selected, disabled).

Show Hint

Practice

(1/5)
1. Which Selenium method checks if a web element is visible on the page?
easy
A. is_enabled()
B. is_visible()
C. is_selected()
D. is_displayed()

Solution

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

    This method returns True if the element is visible to the user on the page.
  2. Step 2: Compare with other methods

    is_enabled() checks if the element can be interacted with, is_selected() checks selection state, and is_visible() is not a valid Selenium method.
  3. Final Answer:

    is_displayed() -> Option D
  4. Quick Check:

    Visibility check = is_displayed() [OK]
Hint: Visible? Use is_displayed() method [OK]
Common Mistakes:
  • Confusing is_enabled() with visibility
  • Using is_selected() for visibility
  • Assuming is_visible() exists in Selenium
2. Which of the following is the correct syntax to check if a checkbox element is selected in Selenium Python?
easy
A. checkbox.is_selected()
B. checkbox.selected()
C. checkbox.isSelected()
D. checkbox.selected

Solution

  1. Step 1: Recall Selenium Python method names

    In Selenium Python, the method to check selection is is_selected() with underscores and parentheses.
  2. Step 2: Identify incorrect syntax

    selected() and selected are invalid; isSelected() is Java style, not Python.
  3. Final Answer:

    checkbox.is_selected() -> Option A
  4. Quick Check:

    Check selected? Use is_selected() [OK]
Hint: Use is_selected() with parentheses for selection check [OK]
Common Mistakes:
  • Using Java method style isSelected() in Python
  • Omitting parentheses after method name
  • Using property 'selected' instead of method
3. Given the following code snippet, what will be the output if the button is visible but disabled?
button = driver.find_element(By.ID, 'submit')
print(button.is_displayed())
print(button.is_enabled())
medium
A. True\nFalse
B. False\nTrue
C. True\nTrue
D. False\nFalse

Solution

  1. Step 1: Understand is_displayed() output

    Since the button is visible, is_displayed() returns True.
  2. Step 2: Understand is_enabled() output

    The button is disabled, so is_enabled() returns False.
  3. Final Answer:

    True\nFalse -> Option A
  4. Quick Check:

    Visible = True, Enabled = False [OK]
Hint: Visible True, Enabled False if button disabled [OK]
Common Mistakes:
  • Assuming disabled means not visible
  • Mixing up is_displayed() and is_enabled() results
  • Expecting both to be True for visible button
4. You wrote this code to check if a checkbox is selected:
checkbox = driver.find_element(By.NAME, 'agree')
if checkbox.selected():
    print('Selected')
else:
    print('Not selected')
What is the error here?
medium
A. Print statements should be inside quotes
B. Missing parentheses after find_element
C. selected() is not a valid method; should use is_selected()
D. Checkbox element locator is incorrect

Solution

  1. Step 1: Check method name for selection

    The correct method to check selection in Selenium Python is is_selected(), not selected().
  2. Step 2: Verify other code parts

    Parentheses after find_element are correct, locator looks valid, and print statements are properly quoted.
  3. Final Answer:

    selected() is not a valid method; should use is_selected() -> Option C
  4. Quick Check:

    Use is_selected() method for checkbox state [OK]
Hint: Use is_selected(), not selected() method [OK]
Common Mistakes:
  • Using selected() instead of is_selected()
  • Confusing method names with Java syntax
  • Ignoring method parentheses
5. You want to write a test that clicks a button only if it is visible and enabled. Which code snippet correctly checks these conditions before clicking?
button = driver.find_element(By.ID, 'save')
hard
A. if button.is_displayed() or button.is_enabled(): button.click()
B. if button.is_displayed() and button.is_enabled(): button.click()
C. if button.is_selected() and button.is_enabled(): button.click()
D. if button.is_enabled(): button.click()

Solution

  1. Step 1: Identify conditions for clicking

    The button must be visible (is_displayed()) and enabled (is_enabled()) to be safely clicked.
  2. Step 2: Analyze each option

    if button.is_displayed() and button.is_enabled(): button.click() uses both conditions with 'and', which is correct. if button.is_selected() and button.is_enabled(): button.click() wrongly uses is_selected() which is for checkboxes/radios. if button.is_displayed() or button.is_enabled(): button.click() uses 'or' which may click if only one condition is true. if button.is_enabled(): button.click() checks only enabled, ignoring visibility.
  3. Final Answer:

    if button.is_displayed() and button.is_enabled():\n button.click() -> Option B
  4. Quick Check:

    Click only if visible AND enabled [OK]
Hint: Use and with is_displayed() and is_enabled() before click [OK]
Common Mistakes:
  • Using or instead of and for conditions
  • Checking is_selected() for buttons
  • Ignoring visibility check before clicking
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