Automation with playwright - Part Last Miscellenous
Handle New Tab Opened by a Link
When clicking a link that opens a new tab (target=“_blank”), use context.waitForEvent
.
// Start waiting for new page before the click
const pagePromise = context.waitForEvent('page');
// Perform click that opens new tab
await page.locator('a[target="_blank"]').click();
// Wait for and capture the new page
const newPage = await pagePromise;
await newPage.waitForLoadState();