Playwright Focus Element

Some websites have focus events, meaning elements appear only when focused on a particular part. Playwright can handle this focused event with the focus() command.

This command ensures that the element is within focus during the test.



The timeout default is 30secs, and 0 can be passed to skip timeout.


const {test, expect} = require('@playwright/test');
test('Focus', async ({browser})=> 
const context = await browser.newContext();
const page = await context.newPage();
await page.goto("");
await page.pause();
await page.locator('//input[@type="text"]').focus();

  • The focus was made on the input field.
  • The input field is highlighted and focused on with its respective element name.
Tue, 11/29/2022 - 14:04
Ashwin possesses over five years of experience in the Quality Assurance industry and is currently serving as a Technical Lead at iVagus. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.