click0
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-click-btn')
page.getByTestId('sandbox-click-btn')
page.locator('#sandbox-click-btn')
page.locator('css=button#sandbox-click-btn')
page.locator('xpath=//button[@id="sandbox-click-btn"]')
page.getByRole('button', { name: 'click' })
page.getByLabel('Sandbox primary click')
page.getByTitle('Sandbox click')
page.getByText('click', { exact: true })
page.getByPlaceholder('…') — not a text field
page.getByAltText('…') — not an image
page.locator('main').filter({ has: page.getByTestId('sandbox-click-btn') }).getByTestId('sandbox-click-btn')
dblclick0
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-dblclick-btn')
page.getByTestId('sandbox-dblclick-btn')
page.locator('#sandbox-dblclick-btn')
page.locator('css=button#sandbox-dblclick-btn')
page.locator('xpath=//button[@id="sandbox-dblclick-btn"]')
page.getByRole('button', { name: 'dblclick' })
page.getByLabel('Sandbox double click')
page.getByTitle('Sandbox double click')
page.getByText('dblclick', { exact: true })
page.getByPlaceholder('…') — not a text field
page.getByAltText('…') — not an image
page.locator('#playwright-sandbox-main').filter({ hasText: 'dblclick' }).locator('#sandbox-dblclick-btn')
fill / clear
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-fill-input')
page.getByTestId('sandbox-fill-input')
page.locator('#sandbox-fill-input')
page.locator('css=#sandbox-fill-input')
page.locator('xpath=//*[@id="sandbox-fill-input"]')
page.getByRole('textbox', { name: 'Sandbox fill and clear' })
page.getByLabel('Sandbox fill and clear')
page.getByPlaceholder('Sandbox fill placeholder')
page.getByTitle('Sandbox fill input')
page.getByText('…') — use role/label for field
page.getByAltText('…') — not an image
page.locator('main').getByTestId('sandbox-fill-input')
type
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-type-input')
page.getByTestId('sandbox-type-input')
page.locator('#sandbox-type-input')
page.locator('css=#sandbox-type-input')
page.locator('xpath=//*[@id="sandbox-type-input"]')
page.getByRole('textbox', { name: 'Sandbox type field' })
page.getByLabel('Sandbox type field')
page.getByPlaceholder('Sandbox type placeholder')
page.getByTitle('Sandbox type input')
page.getByAltText('…') — not an image
page.locator('div').filter({ has: page.getByTestId('sandbox-type-input') }).getByTestId('sandbox-type-input')
check / uncheck
Locator examples
page.getByTestId('sandbox-check-a') / page.getByTestId('sandbox-check-b')
page.locator('#sandbox-check-a')
page.locator('css=#sandbox-check-a')
page.locator('xpath=//*[@id="sandbox-check-a"]')
page.getByRole('checkbox', { name: 'Sandbox checkbox A' })
page.getByLabel('Sandbox checkbox A')
page.getByText('Sandbox checkbox A') — label text
page.getByPlaceholder('…') — not a text field
page.getByTitle('…') — use title on wrapper if needed
page.getByAltText('…') — not an image
page.locator('label').filter({ has: page.locator('#sandbox-check-a') })
hover
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-hover-target')
page.getByTestId('sandbox-hover-target')
page.locator('#sandbox-hover-target')
page.locator('css=#sandbox-hover-target')
page.locator('xpath=//*[@id="sandbox-hover-target"]')
page.getByText('hover', { exact: true })
page.getByLabel('Sandbox hover target')
page.getByTitle('Sandbox hover box')
page.getByRole('…') — generic div; prefer testId / text
page.getByPlaceholder('…') — not a field
page.getByAltText('…') — not an image
page.locator('div').filter({ hasText: 'hover' }).first()
hover
focus
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-focus-btn')
page.getByTestId('sandbox-focus-btn')
page.locator('#sandbox-focus-btn')
page.locator('css=button#sandbox-focus-btn')
page.locator('xpath=//button[@id="sandbox-focus-btn"]')
page.getByRole('button', { name: 'focus' })
page.getByLabel('Sandbox focus button')
page.getByTitle('Sandbox focus target')
page.getByText('focus', { exact: true })
page.getByPlaceholder('…') — not a text field
page.getByAltText('…') — not an image
page.locator('button').filter({ hasText: 'focus' })
selectOption
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-select')
page.getByTestId('sandbox-select')
page.locator('#sandbox-select')
page.locator('css=#sandbox-select')
page.locator('xpath=//*[@id="sandbox-select"]')
page.getByRole('combobox', { name: 'Sandbox dropdown' })
page.getByLabel('Sandbox dropdown')
page.getByTitle('Sandbox select')
page.getByText('option1') — option text when list open
page.getByPlaceholder('…') — native select has no placeholder
page.getByAltText('…') — not an image
page.locator('select').filter({ has: page.locator('option[value="option1"]') })
setInputFiles
Locator examples
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-file-input')
page.getByTestId('sandbox-file-input')
page.locator('#sandbox-file-input')
page.locator('css=input[type=file]#sandbox-file-input')
page.locator('xpath=//*[@id="sandbox-file-input"]')
page.getByLabel('Sandbox file upload')
page.getByTitle('Sandbox file input')
page.getByRole('button', { name: /Choose|Browse/i }) — browser-dependent
page.getByText('…') — not reliable for file input
page.getByPlaceholder('…') — not used on file input
page.getByAltText('…') — not an image
page.locator('input[type="file"]')
Keyboard
press Enter
Locator examples
page.getByTestId('sandbox-enter-input')
page.locator('#sandbox-enter-input')
page.getByRole('textbox', { name: 'Sandbox Enter key field' })
page.getByLabel('Sandbox Enter key field')
page.getByPlaceholder('Sandbox enter placeholder')
page.getByTitle('Sandbox enter input')
page.locator('css=#sandbox-enter-input')
page.locator('xpath=//*[@id="sandbox-enter-input"]')
page.getByText('ok') — after Enter on #sandbox-enter-log
page.getByAltText('…') — not an image
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-enter-input')
locator.press (Escape, ArrowDown, …)
Locator examples
page.getByTestId('sandbox-keyboard-press-input')
page.locator('#sandbox-keyboard-press-input')
page.getByLabel('Sandbox key press field')
page.getByPlaceholder('Sandbox key press placeholder')
page.getByTitle('Sandbox key press input')
page.getByRole('textbox', { name: 'Sandbox key press field' })
page.getByTestId('sandbox-keyboard-last-key') — assertion target
page.locator('css=#sandbox-keyboard-press-input')
page.locator('xpath=//*[@id="sandbox-keyboard-press-input"]')
page.getByAltText('…') — not an image
page.locator('div').filter({ has: page.getByTestId('sandbox-keyboard-press-input') })
last key:—
page.keyboard.type
Locator examples
page.getByTestId('sandbox-keyboard-type')
page.locator('#sandbox-keyboard-type')
page.getByLabel('Sandbox keyboard type field')
page.getByPlaceholder(/keyboard.type/)
page.getByTitle('Sandbox keyboard type')
page.getByRole('textbox', { name: 'Sandbox keyboard type field' })
page.locator('css=#sandbox-keyboard-type')
page.locator('xpath=//*[@id="sandbox-keyboard-type"]')
page.getByText('…') — use placeholder/label
page.getByAltText('…') — not an image
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-keyboard-type')
page.keyboard.insertText
Locator examples
page.getByTestId('sandbox-keyboard-insert')
page.locator('#sandbox-keyboard-insert')
page.getByLabel('Sandbox keyboard insert field')
page.getByPlaceholder(/insertText/)
page.getByTitle('Sandbox keyboard insert')
page.getByRole('textbox', { name: 'Sandbox keyboard insert field' })
page.locator('css=#sandbox-keyboard-insert')
page.locator('xpath=//*[@id="sandbox-keyboard-insert"]')
page.getByAltText('…') — not an image
page.getByTestId('playwright-sandbox-root').getByTestId('sandbox-keyboard-insert')
press Space (focusable div)
Locator examples
page.getByTestId('sandbox-keyboard-space-target')
page.locator('#sandbox-keyboard-space-target')
page.getByRole('button', { name: /Space count/ })
page.getByLabel('Sandbox space counter')
page.getByTitle('Sandbox space target')
page.locator('css=#sandbox-keyboard-space-target')
page.locator('xpath=//*[@id="sandbox-keyboard-space-target"]')
page.getByText(/Space count/)
page.getByPlaceholder('…') — not a field
page.getByAltText('Sandbox space section mark')
page.getByTestId('playwright-sandbox-root').locator('#sandbox-keyboard-space-target')
Space count: 0
Back link← Back
Locator examples
page.getByRole('link', { name: /Back/i })
page.getByText('← Back')
page.locator('a[href="/practice/examples"]')
page.locator('css=a[href*="practice/examples"]')
