← Back to library
Playwrightselectorsbeginnerv1.20+

getByLabel — fill a form input

Find an input by its associated label text — no need to know the input's id or name attribute.

1test('fills inputs by their label text', async ({ page }) => {
2  await page.goto('/signup');
3
4  // getByLabel works with <label for="...">, aria-label, and aria-labelledby
5  await page.getByLabel('Email address').fill('qa@example.com');
6  await page.getByLabel('Password').fill('Str0ng!Pass');
7
8  await page.getByRole('button', { name: 'Sign up' }).click();
9  await expect(page.getByText('Account created')).toBeVisible();
10});
#getByLabel#label#form#input