Input

Insight

On completion of this exercise, you can learn the following concepts.

  1. sendKeys()
  2. Keyboard TAB
  3. getAttribute()
  4. clear()
  5. isEnabled()

Introduction

Input fields are the foundation of form automation. Common scenarios include:

  1. Type into a fieldsendKeys / fill
  2. Clear a field — remove existing text before typing
  3. Read the current valuegetAttribute("value")
  4. Check disabled inputs — field exists but cannot be edited
  5. Check readonly inputs — field displays text but rejects keyboard input

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Type text sendKeys("text") fill("text") fill("text")
Clear field clear() fill("") fill("")
Read value getAttribute("value") inputValue() input_value()
Is disabled !isEnabled() toBeDisabled() to_be_disabled()
Is readonly getAttribute("readonly") toHaveAttribute("readonly", "") to_have_attribute(...)
Press Tab sendKeys(Keys.TAB) press("Tab") press("Tab")

1. Type text into an input field

Selenium (Java)

driver.findElement(By.id("movieName")).sendKeys("Inception");

Playwright (JS)

await page.fill("#movieName", "Inception");

Playwright (Python)

page.fill("#movieName", "Inception")

2. Append text and press Tab

Selenium (Java)

WebElement field = driver.findElement(By.id("appendText"));
field.sendKeys(" and feeling great");
field.sendKeys(Keys.TAB);

Playwright (JS)

await page.locator("#appendText").fill("I am good and feeling great");
await page.locator("#appendText").press("Tab");

Playwright (Python)

page.locator("#appendText").fill("I am good and feeling great")
page.locator("#appendText").press("Tab")

3. Verify text present inside an input field

Selenium (Java)

String value = driver.findElement(By.id("insideText")).getAttribute("value");
assertEquals("QA PlayGround", value);

Playwright (JS)

await expect(page.locator("#insideText")).toHaveValue("QA PlayGround");

Playwright (Python)

expect(page.locator("#insideText")).to_have_value("QA PlayGround")

4. Clear an input field

Selenium (Java)

WebElement field = driver.findElement(By.id("clearText"));
field.clear();
assertEquals("", field.getAttribute("value"));

Playwright (JS)

await page.locator("#clearText").fill("");
await expect(page.locator("#clearText")).toHaveValue("");

Playwright (Python)

page.locator("#clearText").fill("")
expect(page.locator("#clearText")).to_have_value("")

5. Check an input is disabled

Selenium (Java)

WebElement disabledInput = driver.findElement(By.id("disabledInput"));
assertFalse(disabledInput.isEnabled());

Playwright (JS)

await expect(page.locator("#disabledInput")).toBeDisabled();

Playwright (Python)

expect(page.locator("#disabledInput")).to_be_disabled()

6. Check an input is readonly

Selenium (Java)

WebElement readonlyInput = driver.findElement(By.id("readonlyInput"));
assertNotNull(readonlyInput.getAttribute("readonly"));
// Attempt to type — it won't change
readonlyInput.sendKeys("test");
assertEquals("This text is readonly", readonlyInput.getAttribute("value"));

Playwright (JS)

await expect(page.locator("#readonlyInput")).toHaveAttribute("readonly", "");
await page.locator("#readonlyInput").fill("test"); // no effect
await expect(page.locator("#readonlyInput")).toHaveValue("This text is readonly");

Playwright (Python)

expect(page.locator("#readonlyInput")).to_have_attribute("readonly", "")
page.locator("#readonlyInput").fill("test")  # no effect
expect(page.locator("#readonlyInput")).to_have_value("This text is readonly")

📄 Also Read: Top 10 Best Automation Practice Website