Beginner Chapter 5 · 10 min read

Interactions — Click, Type & Select

Click buttons, type into inputs, select dropdowns, check/uncheck checkboxes, handle radio buttons, and clear fields.

Interacting with Web Elements

After finding elements, you need to interact with them — clicking buttons, typing into text fields, selecting dropdown options, and toggling checkboxes. Selenium provides straightforward methods for all standard form interactions.

Core Interactions

  • click() — Click buttons, links, checkboxes, radio buttons
  • sendKeys() — Type text into input fields and text areas
  • clear() — Clear the current value from an input
  • Select class — Interact with <select> dropdowns (by value, text, or index)
  • getAttribute() — Read element attributes like value, href, class
  • getText() — Get the visible text of an element
InteractionsTest.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.*;

public class InteractionsTest {

    WebDriver driver;

    @BeforeMethod
    public void setup() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(
            java.time.Duration.ofSeconds(10));
        driver.get("https://www.selenium.dev/selenium/web/web-form.html");
    }

    @Test
    public void testTypeAndClear() {
        WebElement textInput = driver.findElement(By.name("my-text"));

        // Type text
        textInput.sendKeys("Hello Selenium 4!");
        Assert.assertEquals(textInput.getAttribute("value"),
            "Hello Selenium 4!");

        // Clear and retype
        textInput.clear();
        Assert.assertEquals(textInput.getAttribute("value"), "");

        textInput.sendKeys("New text");
        Assert.assertEquals(textInput.getAttribute("value"), "New text");
    }

    @Test
    public void testPasswordField() {
        WebElement password = driver.findElement(By.name("my-password"));
        password.sendKeys("secret123");
        // Password field masks the value but it's still accessible
        Assert.assertEquals(password.getAttribute("value"), "secret123");
    }

    @Test
    public void testTextarea() {
        WebElement textarea = driver.findElement(By.name("my-textarea"));
        textarea.clear();
        textarea.sendKeys("Line 1\nLine 2\nLine 3");
        Assert.assertTrue(textarea.getAttribute("value").contains("Line 2"));
    }

    @Test
    public void testSelectDropdown() {
        WebElement selectElement = driver.findElement(By.name("my-select"));
        Select dropdown = new Select(selectElement);

        // Select by visible text
        dropdown.selectByVisibleText("Two");
        Assert.assertEquals(dropdown.getFirstSelectedOption().getText(), "Two");

        // Select by value
        dropdown.selectByValue("3");
        Assert.assertEquals(
            dropdown.getFirstSelectedOption().getAttribute("value"), "3");

        // Select by index
        dropdown.selectByIndex(0);
        Assert.assertEquals(
            dropdown.getFirstSelectedOption().getText(), "Open this select menu");

        // Get all options
        System.out.println("Options: " + dropdown.getOptions().size());
    }

    @Test
    public void testCheckbox() {
        WebElement checkbox = driver.findElement(By.id("my-check-1"));

        // Check state
        boolean wasChecked = checkbox.isSelected();
        System.out.println("Initially checked: " + wasChecked);

        // Toggle
        checkbox.click();
        Assert.assertNotEquals(checkbox.isSelected(), wasChecked);

        // Click again to revert
        checkbox.click();
        Assert.assertEquals(checkbox.isSelected(), wasChecked);
    }

    @Test
    public void testRadioButton() {
        WebElement radio = driver.findElement(By.id("my-radio-1"));

        if (!radio.isSelected()) {
            radio.click();
        }
        Assert.assertTrue(radio.isSelected());
    }

    @Test
    public void testSubmitForm() {
        driver.findElement(By.name("my-text")).sendKeys("Test input");
        WebElement submitBtn = driver.findElement(
            By.cssSelector("button[type='submit']"));
        submitBtn.click();

        // Verify form was submitted
        String pageSource = driver.getPageSource();
        System.out.println("Form submitted. URL: " + driver.getCurrentUrl());
    }

    @AfterMethod
    public void teardown() { if (driver != null) driver.quit(); }
}
test_interactions.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import pytest

@pytest.fixture
def driver():
    d = webdriver.Chrome()
    d.implicitly_wait(10)
    d.get("https://www.selenium.dev/selenium/web/web-form.html")
    yield d
    d.quit()


def test_type_and_clear(driver):
    text_input = driver.find_element(By.NAME, "my-text")

    text_input.send_keys("Hello Selenium 4!")
    assert text_input.get_attribute("value") == "Hello Selenium 4!"

    text_input.clear()
    assert text_input.get_attribute("value") == ""

    text_input.send_keys("New text")
    assert text_input.get_attribute("value") == "New text"


def test_password_field(driver):
    password = driver.find_element(By.NAME, "my-password")
    password.send_keys("secret123")
    assert password.get_attribute("value") == "secret123"


def test_textarea(driver):
    textarea = driver.find_element(By.NAME, "my-textarea")
    textarea.clear()
    textarea.send_keys("Line 1\nLine 2\nLine 3")
    assert "Line 2" in textarea.get_attribute("value")


def test_select_dropdown(driver):
    select_el = driver.find_element(By.NAME, "my-select")
    dropdown = Select(select_el)

    # Select by visible text
    dropdown.select_by_visible_text("Two")
    assert dropdown.first_selected_option.text == "Two"

    # Select by value
    dropdown.select_by_value("3")
    assert dropdown.first_selected_option.get_attribute("value") == "3"

    # Select by index
    dropdown.select_by_index(0)

    print(f"Options: {len(dropdown.options)}")


def test_checkbox(driver):
    checkbox = driver.find_element(By.ID, "my-check-1")
    was_checked = checkbox.is_selected()

    checkbox.click()
    assert checkbox.is_selected() != was_checked

    checkbox.click()
    assert checkbox.is_selected() == was_checked


def test_radio_button(driver):
    radio = driver.find_element(By.ID, "my-radio-1")
    if not radio.is_selected():
        radio.click()
    assert radio.is_selected()


def test_submit_form(driver):
    driver.find_element(By.NAME, "my-text").send_keys("Test input")
    driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
    print(f"Form submitted. URL: {driver.current_url}")

Selenium Beginner Interactions — Click, Type & Select

Written by PV

© 2026 All Rights Reserved