Skip to content Skip to sidebar Skip to footer

How To Open The New Tab Using Playwright (ex. Click The Button To Open The New Section In A New Tab)

I am looking for a simpler solution to a current situation. For example, you open the google (any another website) and you want BY CLICK on the button (ex. Gmail) - open this page

Solution 1:

it('Open a new tab', async function () {
     await page.click(button, { button: "middle" });
     await page.waitForTimeout(); //waitForNavigation and waitForLoadState do not work in this case
     let pages = await context.pages();
     expect(await pages[1].title()).equal('Title');

Solution 2:

You could pass a modifier to the click function. In macos it would be Meta because you'd open in a new tab with cmd+click. In windows it would be Control.

const browser = await playwright["chromium"].launch({headless : false});
const page = await browser.newPage();
await page.goto('https://www.facebook.com/');
var pagePromise = page.context().waitForEvent('page', p => p.url() =='https://www.messenger.com/');
await page.click('text=Messenger', { modifiers: ['Meta']});
const newPage = await pagePromise;
await newPage.bringToFront();
await browser.close();

Post a Comment for "How To Open The New Tab Using Playwright (ex. Click The Button To Open The New Section In A New Tab)"