Cannot open new tab in the same context in Playwright (c#)

306 Views Asked by At

I'm trying to open new tab in the same browser session and switch context to it.

When I do it with following code:

await BrowserSession.Browser.Contexts[0].NewPageAsync();

I got the error: Please use Browser.NewContextAsync()


await BrowserSession.Browser.NewPageAsync();

from other hand create new BrowserContext and open new page in separate window (not new tab).

1

There are 1 best solutions below

0
BernardV On

Playwright's documentation states:

Page provides methods to interact with a single tab in a Browser... One Browser instance might have multiple Page instances.

https://playwright.dev/dotnet/docs/api/class-page

Using the above you should be able to open another tab using something like this:

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Webkit.LaunchAsync();

var tabOne = await browser.NewPageAsync();
await tabOne.GotoAsync("https://www.theverge.com");

var tabTwo = await browser.NewPageAsync();
await tabTwo.GotoAsync("https://www.google.com");