Get Input, Textarea, Radio, Checkbox and Select Dropdown Values in Playwright (JavaScript)
In Playwright, you can retrieve form element values using methods such as:
inputValue()– Get input and textarea values.isChecked()– Check whether a checkbox or radio button is selected.inputValue()– Get the selected value from a dropdown.textContent()– Get text content from an element.allTextContents()– Get text from multiple elements.
Let's understand each with examples and output.
1. Get Input Field Value
Use inputValue() to retrieve the value entered into an input field.
HTML
<input type="text" id="username" value="Sumit">
<input type="email" id="email" value="sumit@gmail.com">
<input type="password" id="password" value="123456">
Playwright JavaScript
import { test, expect } from '@playwright/test';
test('Get input field values', async ({ page }) => {
await page.goto('https://example.com');
const username = await page.locator('#username').inputValue();
const email = await page.locator('#email').inputValue();
const password = await page.locator('#password').inputValue();
console.log('Username:', username);
console.log('Email:', email);
console.log('Password:', password);
});
Output
Username: Sumit
Email: sumit@gmail.com
Password: 123456
2. Get Textarea Value
Use inputValue() to retrieve the text entered inside a textarea.
HTML
<textarea id="address">
Bhubaneswar, Odisha
</textarea>
Playwright JavaScript
test('Get textarea value', async ({ page }) => {
await page.goto('https://example.com');
const address = await page.locator('#address').inputValue();
console.log('Address:', address);
});
Output
Address: Bhubaneswar, Odisha
3. Get Radio Button Value
Use inputValue() to get the value attribute of a radio group and isChecked() to check which radio button is selected.
HTML
<label>
<input type="radio" name="gender" value="Male">
Male
</label>
<label>
<input type="radio" name="gender" value="Female">
Female
</label>
<label>
<input type="radio" name="gender" value="Other">
Other
</label>
Playwright JavaScript
test('Get selected radio button value', async ({ page }) => {
await page.goto('https://example.com');
const radioButtons = page.locator('input[name="gender"]');
const count = await radioButtons.count();
for (let i = 0; i < count; i++) {
const radio = radioButtons.nth(i);
if (await radio.isChecked()) {
const value = await radio.getAttribute('value');
console.log('Selected Gender:', value);
}
}
});
Output
Selected Gender: Male
4. Get Checkbox Value
Use isChecked() to check whether a checkbox is selected. Use getAttribute('value') to retrieve its value attribute.
HTML
<label>
<input type="checkbox" name="skills" value="Java" checked>
Java
</label>
<label>
<input type="checkbox" name="skills" value="Python" checked>
Python
</label>
<label>
<input type="checkbox" name="skills" value="Playwright">
Playwright
</label>
<label>
<input type="checkbox" name="skills" value="Selenium" checked>
Selenium
</label>
Playwright JavaScript
test('Get selected checkbox values', async ({ page }) => {
await page.goto('https://example.com');
const checkboxes = page.locator('input[name="skills"]');
const count = await checkboxes.count();
let selectedSkills = [];
for (let i = 0; i < count; i++) {
const checkbox = checkboxes.nth(i);
if (await checkbox.isChecked()) {
const value = await checkbox.getAttribute('value');
selectedSkills.push(value);
}
}
console.log('Selected Skills:', selectedSkills);
});
Output
Selected Skills: [ 'Java', 'Python', 'Selenium' ]
5. Get Select Dropdown Value
Use inputValue() to get the currently selected option value.
HTML
<select id="country">
<option value="India" selected>India</option>
<option value="USA">United States</option>
<option value="UK">United Kingdom</option>
<option value="Canada">Canada</option>
</select>
Playwright JavaScript
test('Get dropdown selected value', async ({ page }) => {
await page.goto('https://example.com');
const country = await page.locator('#country').inputValue();
console.log('Selected Country:', country);
});
Output
Selected Country: India
Get Dropdown Selected Text
If you want to retrieve the visible text instead of the option value, use locator('option:checked').textContent().
const selectedText = await page
.locator('#country option:checked')
.textContent();
console.log('Selected Text:', selectedText);
Output
Selected Text: India
6. Get All Dropdown Options
Use allTextContents() to retrieve all dropdown option texts.
test('Get all dropdown options', async ({ page }) => {
await page.goto('https://example.com');
const options = await page.locator('#country option').allTextContents();
console.log('Dropdown Options:', options);
});
Output
Dropdown Options: [
'India',
'United States',
'United Kingdom',
'Canada'
]
7. Complete Example – Get All Form Values
Here is one example that retrieves input, textarea, radio, checkbox and dropdown values together.
HTML
<form>
<input type="text" id="username" value="Sumit">
<textarea id="address">Bhubaneswar</textarea>
<input type="radio" name="gender" value="Male" checked>
<input type="radio" name="gender" value="Female">
<input type="checkbox" name="skills" value="Java" checked>
<input type="checkbox" name="skills" value="Python" checked>
<input type="checkbox" name="skills" value="Playwright">
<select id="country">
<option value="India" selected>India</option>
<option value="USA">USA</option>
</select>
</form>
Playwright JavaScript
import { test, expect } from '@playwright/test';
test('Get all form field values', async ({ page }) => {
await page.goto('https://example.com');
// Get input value
const username = await page.locator('#username').inputValue();
// Get textarea value
const address = await page.locator('#address').inputValue();
// Get selected radio value
const radioButtons = page.locator('input[name="gender"]');
let gender = '';
for (let i = 0; i < await radioButtons.count(); i++) {
const radio = radioButtons.nth(i);
if (await radio.isChecked()) {
gender = await radio.getAttribute('value');
break;
}
}
// Get selected checkbox values
const checkboxes = page.locator('input[name="skills"]');
let skills = [];
for (let i = 0; i < await checkboxes.count(); i++) {
const checkbox = checkboxes.nth(i);
if (await checkbox.isChecked()) {
skills.push(await checkbox.getAttribute('value'));
}
}
// Get dropdown value
const country = await page.locator('#country').inputValue();
// Display all values
console.log('Username:', username);
console.log('Address:', address);
console.log('Gender:', gender);
console.log('Skills:', skills);
console.log('Country:', country);
});
Output
Username: Sumit
Address: Bhubaneswar
Gender: Male
Skills: [ 'Java', 'Python' ]
Country: India
8. Important Playwright Methods Cheat Sheet
| Method | Purpose |
|---|---|
inputValue() |
Get input, textarea or select value |
getAttribute('value') |
Get element value attribute |
isChecked() |
Check checkbox or radio selection |
isVisible() |
Check element visibility |
isEnabled() |
Check element enabled state |
textContent() |
Get element text |
innerText() |
Get visible rendered text |
allTextContents() |
Get text from multiple elements |
count() |
Get number of matching elements |
getAttribute('checked') |
Get checked HTML attribute |
Important: For checkbox and radio buttons, prefer isChecked() rather than checking the HTML checked attribute. The attribute represents the initial HTML state and may not reflect the current selection.
9. Quick Interview Questions
- How do you get an input field value in Playwright?
- What is the difference between
inputValue()andtextContent()? - How do you get selected checkbox values?
- How do you identify which radio button is selected?
- How do you get the selected dropdown option text?
- How do you get all dropdown options?
- Can we use
inputValue()on a textarea? - How do you get multiple checkbox values using a loop?
Interview Tip
Remember these three important methods:
await locator.inputValue(); // Input, textarea, select
await locator.isChecked(); // Checkbox, radio
await locator.textContent(); // Element text
These cover most form-value retrieval scenarios in Playwright automation.

No comments:
Post a Comment