How to use ScreenshotAsync method of PuppeteerSharp.Page class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.ScreenshotAsync

LottoXClient.cs

Source:LottoXClient.cs Github

copy

Full Screen

...145 }146 private async Task TakeTopOrderScreenshot(string filepath)147 {148 Page page = await GetPage();149 await page.ScreenshotAsync(filepath,150 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 400, X = 1000, Y = 350 } });151 }152 private async Task TakeOrdersScreenshot(string filepath)153 {154 Page page = await GetPage();155 await page.ScreenshotAsync(filepath,156 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 983, Height = 1440, X = 1017 } });157 }158 private async Task TakeHeaderScreenshot(string filepath)159 {160 Page page = await GetPage();161 await page.ScreenshotAsync(filepath,162 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 350, X = 250 } });163 }164 private async Task TakeQuantityColumnScreenshot(string filePath)165 {166 Page page = await GetPage();167 await page.ScreenshotAsync(filePath,168 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 65, Height = 350, X = 477, Y = 400 } });169 }170 private async Task TakePortfolioScreenshot(string filePath)171 {172 Page page = await GetPage();173 await page.ScreenshotAsync(filePath,174 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 1440 } });175 }176 }177}...

Full Screen

Full Screen

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...58 {59 fileName = fileName.Replace(".jpg", $"{DateTime.Now.ToString("ffff")}.jpg");60 }6162 await page.ScreenshotAsync(fileName, screenshotOptions);6364 if (System.IO.File.Exists(fileName))65 {66 BitmapImage bi = new BitmapImage();67 bi.BeginInit();68 bi.UriSource = new Uri(fileName, UriKind.Absolute);69 bi.EndInit();70 this.image.Source = bi;71 }72 else73 {74 EMessageBox.Show("保存网页截图失败");75 }76 ...

Full Screen

Full Screen

PuppeteerWrapper.cs

Source:PuppeteerWrapper.cs Github

copy

Full Screen

...72 case SourceType.URL:73 {74 var page = await browser.NewPageAsync();75 await page.GoToAsync("http://www.google.com");76 await page.ScreenshotAsync(outputPath + "\\" + fileName);77 break;78 }79 case SourceType.HTML_FILE:80 {81 var page = await browser.NewPageAsync();82 await page.GoToAsync(sourceData);83 await page.ScreenshotAsync(outputPath + "\\" + fileName);84 break;85 }86 case SourceType.HTML_STRING:87 {88 using (var page = await browser.NewPageAsync())89 {90 await page.SetContentAsync(sourceData);91 var result = await page.GetContentAsync();92 await page.ScreenshotAsync(outputPath + "\\" + fileName);93 }94 break;95 }96 }97 }98 public async Task GetScreenshots(string fileName, ViewPortOptions viewPortOptions)99 {100 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);101 var browser = await Puppeteer.LaunchAsync(new LaunchOptions102 {103 Headless = true104 });105 switch (sourceType)106 {107 case SourceType.URL:108 {109 var page = await browser.NewPageAsync();110 await page.GoToAsync("http://www.google.com");111 await page.SetViewportAsync(viewPortOptions);112 await page.ScreenshotAsync(outputPath + "\\" + fileName);113 break;114 }115 case SourceType.HTML_FILE:116 {117 var page = await browser.NewPageAsync();118 await page.GoToAsync(sourceData);119 await page.SetViewportAsync(viewPortOptions);120 await page.ScreenshotAsync(outputPath + "\\" + fileName);121 break;122 }123 case SourceType.HTML_STRING:124 {125 using (var page = await browser.NewPageAsync())126 {127 await page.SetContentAsync(sourceData);128 var result = await page.GetContentAsync();129 await page.SetViewportAsync(viewPortOptions);130 await page.ScreenshotAsync(outputPath + "\\" + fileName);131 }132 break;133 }134 }135 }136 }137}

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...53 screenshotOptions.FullPage = true; //是否截取整个页面54 screenshotOptions.OmitBackground = false;//是否使用透明背景,而不是默认白色背景55 screenshotOptions.Quality = 100; //截图质量 0-100(png不可用)56 screenshotOptions.Type = ScreenshotType.Jpeg; //截图格式 57 await page.ScreenshotAsync(path,screenshotOptions);58 MessageBox.Show($"截图已经保存至{path}");59 }60 private async void button3_Click(object sender, EventArgs e)61 {62 //获取最后的标签页63 var page = (await browser.PagesAsync()).Last();64 var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.pdf");65 //设置PDF选项66 PdfOptions pdfOptions = new PdfOptions();67 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚68 pdfOptions.FooterTemplate = ""; //页脚文本69 pdfOptions.Format = new PuppeteerSharp.Media.PaperFormat(8.27m,11.69m); //pdf纸张格式 英寸为单位 70 pdfOptions.HeaderTemplate = ""; //页眉文本71 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平 ...

Full Screen

Full Screen

BookingScrapperTest.cs

Source:BookingScrapperTest.cs Github

copy

Full Screen

...34 await page.SetCookieAsync(cookie);35 }36 }37 await page.GoToAsync("https://admin.booking.com");38 await page.ScreenshotAsync(screenshotFilePath);39 await page.Keyboard.TypeAsync("2453996");40 await page.ClickAsync("form.nw-signin button[type=\"submit\"]");41 var passwordInput = await page.WaitForSelectorAsync("input#password");42 await passwordInput.TypeAsync("booking-johnnyx1A");43 var passwordSubmitButton = await page.WaitForSelectorAsync("form.nw-signin button[type=\"submit\"]");44 navigationTask = page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });45 await passwordSubmitButton.ClickAsync();46 await navigationTask;47 await page.ScreenshotAsync(screenshotFilePath);48 if(page.Url.Contains("ses="))49 {50 return new CookieParam[2];51 }52 else53 {54 var pulseButton = await page.WaitForSelectorAsync("a.nw-pulse-verification-link");55 await pulseButton.ClickAsync();56 var pulseCodeInput = await page.WaitForSelectorAsync("input#sms_code");57 await pulseCodeInput.TypeAsync("706860");58 await page.ScreenshotAsync(screenshotFilePath);59 var pulseCodeSubmitButton = await page.WaitForSelectorAsync("form.nw-tfa-form button[type=\"submit\"]");60 navigationTask = page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });61 await pulseCodeSubmitButton.ClickAsync();62 await navigationTask;63 await page.ScreenshotAsync(screenshotFilePath);64 return await page.GetCookiesAsync();65 }66 67 }68 [TestMethod]69 public async Task DownloadReservations()70 {71 var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "reservations.xls");72 var client = new HttpClient();73 client.DefaultRequestHeaders.Host = "admin.booking.com";74 client.DefaultRequestHeaders.Add("Cookie", "esadm=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPynjCLxCQ5SnKFbrr57DvXZw1sp2yFYUddAZCaighbCs%3D; ecid=WItRz%2BR36BGkGg028uL7CQan; bkng_bfp=aef6b2e1b76f06aacb6f0c2b8f99a513; auth_token=6412390550; ux=e; hauavc=01be565IQWtYpsK1NdHtF%2BjRchlkDi%2FoOolSuOOJywu2bUemY0; bkng_bfp=86f162fcd73527f33dc06228d1d588a0; external_host=account.booking.com; xp=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPpjNyy1T7WUQFoQgXZsIpfhJITCOuSk2SV8Ljk3HCReM%3D; liteha=%5B%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22home%22%7D%2C%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22finance_reservations%22%7D%2C%7B%22actions%22%3A%5B%5D%2C%22page%22%3A%22search_reservations%22%7D%5D; bkng_iam_rt=CAESQlJAKiYcMd1HOoS9iVxT8CQ0oRWVsb4cqGtRVYwapJXGqcQvKA824B18ua8LSvtXsG0V5U3sUlr95FYOhPcqZ_cHCw; he=02UmFuZG9tSVYkc2RlIyh9YbxZGyl9Y5%2BPMzBUOmE1%2BZnRjE6ccsHLcr3XeaLhlmueT7Ji33TyEzQ%3D");75 var data = await client.GetByteArrayAsync(@"https://admin.booking.com/fresa/extranet/reservations/download?date_type=arrival&date_to=2019-01-10&date_from=2019-12-31&lang=xu&hotel_id=2453996&ses=4e1e6fd18bafca6079649931b72438c6");76 File.WriteAllBytes(filePath, data);77 }...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...3536 37 var element = await page.QuerySelectorAsync("#MainContent_ASPxRoundPanel2_WebChartControl1_IMG");38 //var element = await page.EvaluateExpressionAsync<ElementHandle>("document.getElementById('MainContent_ASPxRoundPanel2_WebChartControl1_IMG')");39 await element.ScreenshotAsync("gurka.png");4041 //await page.ScreenshotAsync("test.png", new ScreenshotOptions42 //{43 // Clip = new Clip()44 // {45 // X = 10,46 // Y = 10,47 // Height = 100,48 // Width = 10049 // }50 //});515253 var res= await page.EvaluateExpressionAsync<string>("document.getElementById('MainContent_signalpagedailycommentarytext').textContent");5455 element = await page.QuerySelectorAsync("#MainContent_signalpagedailycommentarytext"); ...

Full Screen

Full Screen

PageExtensions.cs

Source:PageExtensions.cs Github

copy

Full Screen

...40 if (deltaInfo.Exists)41 {42 deltaInfo.Delete();43 }44 await page.ScreenshotAsync(actualPath).ConfigureAwait(false);45 using var actualBitmap = new Bitmap(actualPath);46 TestContext.AddTestAttachment(actualPath);47 var expectedInfo = new FileInfo(expectedPath);48 if (!expectedInfo.Exists)49 {50 // if this is the first time the test has executed then assume the current screenshot is expected51 actualBitmap.Save(expectedPath);52 }53 using var expectedBitmap = new Bitmap(expectedPath);54 var compare = actualBitmap.Compare(expectedBitmap);55 if (compare != null)56 {57 compare.Save(deltaPath);58 TestContext.AddTestAttachment(deltaPath);...

Full Screen

Full Screen

ImageService.cs

Source:ImageService.cs Github

copy

Full Screen

...17 BrowserWSEndpoint = _config.GetSection(BrowserWSKey).Value18 });19 var page = await browser.NewPageAsync();20 await page.GoToAsync("https://www.google.com");21 await page.ScreenshotAsync("screenshot.png");22 }23 }24}...

Full Screen

Full Screen

ScreenshotAsync

Using AI Code Generation

copy

Full Screen

1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",4}))5{6 var page = await browser.NewPageAsync();7 await page.ScreenshotAsync("google.png");8}9using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions10{11 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",12}))13{14 var page = await browser.NewPageAsync();15 await page.ScreenshotAsync("google.png");16}17using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions18{19 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",20}))21{22 var page = await browser.NewPageAsync();23 await page.ScreenshotAsync("google.png");24}25using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions26{27 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",28}))29{30 var page = await browser.NewPageAsync();31 await page.ScreenshotAsync("google.png");32}33using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions34{35 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",36}))37{38 var page = await browser.NewPageAsync();39 await page.ScreenshotAsync("google.png");40}

Full Screen

Full Screen

ScreenshotAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static async Task Main(string[] args)8 {9 var options = new LaunchOptions { Headless = true };10 using (var browser = await Puppeteer.LaunchAsync(options))11 {12 using (var page = await browser.NewPageAsync())13 {14 var screenshot = await page.ScreenshotAsync();15 await File.WriteAllBytesAsync("screenshot.png", screenshot);16 }17 }18 }19 }20}21using System;22using System.IO;23using System.Threading.Tasks;24using PuppeteerSharp;25{26 {27 static async Task Main(string[] args)28 {29 var options = new LaunchOptions { Headless = true };30 using (var browser = await Puppeteer.LaunchAsync(options))31 {32 using (var page = await browser.NewPageAsync())33 {

Full Screen

Full Screen

ScreenshotAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.ScreenshotAsync("1.png");3var page = await browser.NewPageAsync();4await page.ScreenshotAsync("2.png");5var page = await browser.NewPageAsync();6await page.ScreenshotAsync("3.png");7var page = await browser.NewPageAsync();8await page.ScreenshotAsync("4.png");9var page = await browser.NewPageAsync();10await page.ScreenshotAsync("5.png");11var page = await browser.NewPageAsync();12await page.ScreenshotAsync("6.png");13var page = await browser.NewPageAsync();14await page.ScreenshotAsync("7.png");15var page = await browser.NewPageAsync();16await page.ScreenshotAsync("8.png");17var page = await browser.NewPageAsync();18await page.ScreenshotAsync("9.png");19var page = await browser.NewPageAsync();20await page.ScreenshotAsync("10.png");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Page

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful