1

Below code open Chrome in non maximised state. I can see the code online/youtube used in Java to change the screensize but not found anything for c#.

        public async Task Test1()
        {
            using var playwright = await Playwright.CreateAsync();
            await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
            {
                Headless = false,
            });
            var context = await browser.NewContextAsync();
            // Open new page
            var page = await context.NewPageAsync();
            await page.GotoAsync("https://google.com/");
        }

3 Answers3

0

To start the browser maximized, you need to pass null instead of width & height.

I don't have .NET installed, but it should be something like

await using
var context = await browser.NewContextAsync(new BrowserNewContextOptions {
    ViewportSize = new ViewportSize() null
});
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
0

I found that ViewportSize wouldn't accept null but passing in approximate values for a maximised screen got me the same result.

BrowserNewContextOptions browserNewContextOptions = new BrowserNewContextOptions();
browserNewContextOptions.ViewportSize = new ViewportSize { Height = 1000, Width = 1900 };
IBrowserContext Context = await browser.NewContextAsync(browserNewContextOptions);
Nick Graham
  • 1,311
  • 3
  • 14
  • 21
0

You need two things:

  1. Pass the --start-maximized flag to the Args of LaunchOptions
  2. Override the context-viewport default configuration with NoViewport
public async Task Test1()
{
    var launchOptions = new BrowserTypeLaunchOptions
    {
       Headless = false,
       Args = new List<string> { "--start-maximized" }
    };

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

    var context = await browser.NewContextAsync(new BrowserNewContextOptions
    {
        ViewportSize = ViewportSize.NoViewport
    });

    var page = await context.NewPageAsync();

    await page.GotoAsync("https://google.com/");
}
I.sh.
  • 252
  • 3
  • 13