Based on Puppeteer extra sharp and Node.js Playwright extra
Long way
// Initialization plugin builder
var playwrightExtra = new PlaywrightExtra(BrowserTypeEnum.Chromium);
// Install browser
playwrightExtra.Install();
// Use stealth plugin
playwrightExtra.Use(new StealthExtraPlugin());
// Launch the puppeteer browser with plugins
await playwrightExtra.LaunchAsync(new ()
{
Headless = false
});
// Create a new page
var page = await playwrightExtra.NewPageAsync();
await page.GoToAsync("http://google.com");
Compact way
// Initialize, install, use plugin and launch
var playwrightExtra = await new PlaywrightExtra(BrowserTypeEnum.Chromium)
.Install()
.Use(new StealthExtraPlugin())
.LaunchAsync(new ()
{
Headless = false
});
// Create a new page
var page = await playwrightExtra.NewPageAsync();
await page.GoToAsync("http://google.com");
Because of how Playwright behaves with pages, you should only create new pages through PlaywrightExtra wrapper and not IBrowser. Please refer to examples above.
There are 4 different ways to work with context in PlaywrightExtra:
- launch incognito (as default was) with permanent context
.LaunchAsync(new ()
{
Headless = false
},
persistContext: true)
- launch incognito with new context per page (so when page is closed, context is disposed)
.LaunchAsync(new ()
{
Headless = false
},
persistContext: false)
- launch persistent (user data dir is considered) with permanent context
.LaunchPersistentAsync(new ()
{
Headless = false
},
persistContext: true)
- launch persistent with new context per page
.LaunchPersistentAsync(new ()
{
Headless = false
},
persistContext: false)
While running persistent mode you can specify path to user data directory.
When context is persisted, specify directory at launch:
.LaunchPersistentAsync(new ()
{
Headless = false
},
persistContext: true,
userDataDir: "/path/to/userdir")
When context is created for each page (persistContext: false
), specify directory at page creation:
var page = await _playwrightExtra.NewPageAsync(userDataDir: "/path/to/userdir");
For convenience, you can use helper method when performing request:
await page.GotoAndWaitForIdleAsync("http://google.com/",
idleTime: TimeSpan.FromMilliseconds(1000));
Basically this works like if you perform request with option WaitUntil = WaitUntilState.NetworkIdle
But unlike builtin method, you can specify amount of time to wait after last request has fired.