puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

Can't change resolution

Open thomasd3 opened this issue 2 years ago • 3 comments

The resolution of the viewport stays the same (F# code)

// set the viewport
let viewportOptions = ViewPortOptions(Width = 1920, Height = 1080, IsLandscape = true)
info $"before viewport set to {page.Viewport.Width}x{page.Viewport.Height}"
let! x = page.SetViewportAsync(viewportOptions) |> Async.AwaitTask
info $"after viewport set to {page.Viewport.Width}x{page.Viewport.Height}"

and the output is:

[12:56:30.308] before viewport set to 1000x1000
[12:56:30.339] after viewport set to 1000x1000

This is the main code (F# code):

    let takeScreenshotAsync url width height =
        asyncResult {
            // create the instance
            use browserFetcher = new BrowserFetcher()

            // download the browser, if needed
            let! browser = browserFetcher.DownloadAsync() |> Async.AwaitTask
            info $"using {browser.Browser} version {browser.BuildId} on {browser.Platform}"

            // instantiate the browser
            let launchOptions = LaunchOptions(
                    Headless = true,
                    DefaultViewport = ViewPortOptions(Width = width, Height = height)
                )
            let! browser = Puppeteer.LaunchAsync(launchOptions) |> Async.AwaitTask

            // open a page
            let! page = browser.NewPageAsync() |> Async.AwaitTask

            // set the viewport
            let viewportOptions = ViewPortOptions(Width = width, Height = height, IsLandscape = true)
            do! page.SetViewportAsync(viewportOptions) |> Async.AwaitTask

            // go to the url
            do! page.GoToAsync(url) |> Async.AwaitTask |> Async.Ignore

            // make a screenshot
            let filename = "output.png"
            do! page.ScreenshotAsync(filename) |> Async.AwaitTask

            return ()

and the resolution stays to 1000x1000 If I change the DeviceScaleFactor, the resolution of the screenshot increases, but I still can't change the aspect ratio, the output is always square.

thomasd3 avatar Nov 08 '23 12:11 thomasd3

That's odd. The Viewport property is set with the option you pass. See.

kblok avatar Nov 08 '23 13:11 kblok

I'm going to check if it's not an issue with f#/async where the thread switches and the call is not finished yet

thomasd3 avatar Nov 08 '23 14:11 thomasd3

when I upgraded from 6.2.0 to 13.0.2, the ar-sa html content screenshot does not work as before, I think I may have the same issue, while when running with Headless: false, it seemed to work as expected

WeihanLi avatar Jan 12 '24 09:01 WeihanLi