microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

How can I use Playwright to return some screenshots with WebView2 #2948

Closed zydjohnHotmail closed 4 months ago

zydjohnHotmail commented 4 months ago

Please do not submit this issue.

zydjohnHotmail commented 4 months ago

Hi, I can use WebView2's function: CapturePreviewAsync to take screenshot of some areas in WebView2 browser. I want to know if I can use Playwright to control WebView2 to take 2 screenshots of specific areas: the first is: at coordinate(100, 100) with size of 50 by 50 pixels; the second is at coordinate(200, 200) with size of 150 by 30 pixels; then save the 2 areas in 2 different PictureBox, one is called: PB1, another is called: PB2. If it is possible, please show me the C# code how to this. I am using WebView2 latest version published on: 2024/06/19, but the x64 run time for the new WebView2 version is not available yet. Please advise,

mxschmitt commented 4 months ago

Yes, you can follow our WebView2 guide and then use our normal screenshot functionality to take screenshots: https://playwright.dev/dotnet/docs/screenshots

zydjohnHotmail commented 4 months ago

Hi, I look at the link: https://playwright.dev/dotnet/docs/screenshots It only has code to take screenshot of the full page or elements, but is possible to take screenshot for specific areas, like: the first is: at coordinate(100, 100) with size of 50 by 50 pixels; the second is at coordinate(200, 200) with size of 150 by 30 pixels; then save the 2 areas in 2 different PictureBox, one is called: PB1, another is called: PB2. If you know how to do this, please share some code samples. Thanks,

mxschmitt commented 4 months ago

You can clip, see here for the API docs: https://playwright.dev/dotnet/docs/api/class-page#page-screenshot

        byte[] screenshot = await page.ScreenshotAsync(new()
        {
            Clip = new()
            {
                X = 10,
                Y = 10,
                Width = 100,
                Height = 150
            }
        });
zydjohnHotmail commented 4 months ago

OK, thanks! I will try!