How to use DownloadAsync method of PuppeteerSharp.BrowserFetcher class

Best Puppeteer-sharp code snippet using PuppeteerSharp.BrowserFetcher.DownloadAsync

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...33 {34 //如果这一步发生异常了,可以参考 https://www.cnblogs.com/zhaotianff/p/13528507.html35 try36 {37 await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);3839 var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions40 {41 Headless = true42 });4344 var page = await browser.NewPageAsync(); //打开一个新标签45 await page.GoToAsync(this.tbox_Url.Text); //访问页面4647 //设置截图选项48 PuppeteerSharp.ScreenshotOptions screenshotOptions = new PuppeteerSharp.ScreenshotOptions();49 //screenshotOptions.Clip = new PuppeteerSharp.Media.Clip() { Height = 0, Width = 0, X = 0, Y = 0 };//设置截剪区域50 screenshotOptions.FullPage = true; //是否截取整个页面51 screenshotOptions.OmitBackground = false;//是否使用透明背景,而不是默认白色背景52 screenshotOptions.Quality = 100; //截图质量 0-100(png不可用)53 screenshotOptions.Type = PuppeteerSharp.ScreenshotType.Jpeg; //截图格式5455 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.jpg";5657 if (System.IO.File.Exists(fileName))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 }7677 //在最后记得关闭浏览器及释放资源78 browser.Disconnect();79 browser.Dispose();80 }81 catch(Exception ex)82 {83 EMessageBox.Show(ex.Message);84 }85 }8687 private async void btn_SaveAsPDF_Click(object sender, RoutedEventArgs e)88 {89 //打开网页的操作跟上面是一样的90 try91 {92 await new PuppeteerSharp.BrowserFetcher().DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);93 var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions94 {95 Headless = true96 });97 var page = await browser.NewPageAsync(); //打开一个新标签98 await page.GoToAsync(this.tbox_Url.Text); //访问页面99100 //设置PDF选项101 PuppeteerSharp.PdfOptions pdfOptions = new PuppeteerSharp.PdfOptions();102 pdfOptions.DisplayHeaderFooter = false; //是否显示页眉页脚103 pdfOptions.FooterTemplate = ""; //页脚文本104105 var width = await page.EvaluateFunctionAsync<int>("function getWidth(){return document.body.scrollWidth}");106 var height = await page.EvaluateFunctionAsync<int>("function getHeight(){return document.body.scrollHeight}"); ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...18 InitializeComponent();19 }20 private async void button1_Click(object sender, EventArgs e)21 { 22 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);23 browser = await Puppeteer.LaunchAsync(new LaunchOptions24 {25 Headless = true26 });27 //如果await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);执行不成功28 //可以手动访问https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Win_x64/,下载Chromium浏览器,并解压到指定位置29 //再通过以下代码初始化30 /*31 * LaunchOptions options = new LaunchOptions();32 options.Headless = true;33 options.DefaultViewport = null;34 //忽略证书错误35 options.IgnoreHTTPSErrors = true;36 //chromePath就是下载的Chromium浏览器解压的位置 37 options.ExecutablePath = chromePath;38 browser = await Puppeteer.LaunchAsync(options);39 */40 var page = await browser.NewPageAsync();41 await page.GoToAsync(this.textBox1.Text);...

Full Screen

Full Screen

GistGithubService.cs

Source:GistGithubService.cs Github

copy

Full Screen

...103 }104 private async Task<Uri> GetRedirectCallbackResult(Uri redirectPage)105 {106 var revisionInfo = await browserFetcher107 .DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);108 using var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions109 {110 Headless = false,111 LogProcess = true,112 DumpIO = true113 });114 var page = await browser.NewPageAsync();115 var redirectResult = await page.GoToAsync(redirectPage.AbsoluteUri);116 var target = await browser.WaitForTargetAsync(e => e.Url.Contains(gitHubModel.RedirectUrl!));117 return new Uri(target.Url);118 }119 }120}...

Full Screen

Full Screen

BrowserProvider.cs

Source:BrowserProvider.cs Github

copy

Full Screen

...36 logger.LogDebug("Download browser");37 using var browserFetcher = new BrowserFetcher(Options.Product);38 if (!string.IsNullOrEmpty(Options.Revision))39 {40 await browserFetcher.DownloadAsync(Options.Revision);41 }42 else43 {44 await browserFetcher.DownloadAsync();45 }46 logger.LogDebug("Browser downloaded");47 }48 logger.LogInformation("Start new Browser");49 return await PuppeteerSharp.Puppeteer.LaunchAsync(50 new LaunchOptions51 {52 Headless = Options.Headless,53 Args = Options.BrowserArgs,54 IgnoreHTTPSErrors = Options.IgnoreHTTPSErrors,55 DefaultViewport = Options.ViewPortOptions56 }, loggerFactory);57 }58}...

Full Screen

Full Screen

Browser.cs

Source:Browser.cs Github

copy

Full Screen

...20 };21 entry.SetAbsoluteExpiration(TimeSpan.FromDays(1));22 Console.WriteLine("Downloading chromium");23 using var browserFetcher = new BrowserFetcher();24 await browserFetcher.DownloadAsync();2526 using (var browser = await Puppeteer.LaunchAsync(options))27 using (var page = await browser.NewPageAsync())28 {29 await page.GoToAsync(url);30 var content = await page.GetContentAsync();31 var graph = OpenGraphNet.OpenGraph.ParseHtml(content);32 return new Metadata33 {34 Image = graph.Image,35 Title = graph.Title,36 Url = graph.Url ?? new Uri(url),37 Description = graph.Metadata["og:description"] != null && graph.Metadata["og:description"].Count > 0 ? graph.Metadata["og:description"][0] : ""38 }; ...

Full Screen

Full Screen

PuppeteerDocumentGenerator.cs

Source:PuppeteerDocumentGenerator.cs Github

copy

Full Screen

...6 public class PuppeteerDocumentGenerator : DocumentGeneratorBase7 {8 public async Task<byte[]> Generate(string html)9 {10 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);11 var browser = await Puppeteer.LaunchAsync(new LaunchOptions12 {13 Headless = true,14 // If needed, we can specify a specific version of Chrome15 //ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",16 //IgnoredDefaultArgs = new string[] { "--disable-extensions" },17 //IgnoreDefaultArgs = true,18 });19 using (var page = await browser.NewPageAsync())20 {21 await page.SetContentAsync(html);22 var pdfData = await page.PdfDataAsync(new PdfOptions23 {24 MarginOptions = new MarginOptions...

Full Screen

Full Screen

PuppeteerConnection.cs

Source:PuppeteerConnection.cs Github

copy

Full Screen

...5 {6 public async Task<T> PuppeteerGrabber<T>(string? url, string? infoCode, string? waitSelector)7 {8 BrowserFetcher browserFetcher = new BrowserFetcher();9 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);10 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 Headless = false,13 IgnoreHTTPSErrors = true,14 /*15 Args = new[]16 {17 "--proxy-server=http://ehmseto_01:YouMeK100@smoproxy:8080/",18 "--no-sandbox",19 "--disable-infobars",20 "--disable-setuid-sandbox",21 "--ignore-certificate-errors",22 }23 */...

Full Screen

Full Screen

PuppeteerSharpConverterCollectionExtensions.cs

Source:PuppeteerSharpConverterCollectionExtensions.cs Github

copy

Full Screen

...7 {8 public static IServiceCollection AddPuppeteerSharpPdfConverter(this IServiceCollection services)9 {10 var browserFetcher = new BrowserFetcher();11 browserFetcher.DownloadAsync().GetAwaiter().GetResult();12 services.AddSingleton<IPdfConverter, PuppeteerSharpConverter>();13 return services;14 }15 }16}...

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1var browserFetcher = new BrowserFetcher();2await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);3var browserFetcher = new BrowserFetcher();4var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);5var browserFetcher = new BrowserFetcher();6var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);7await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);8var browserFetcher = new BrowserFetcher();9var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);11await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);12var browserFetcher = new BrowserFetcher();13var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1var browserFetcher = new BrowserFetcher();2var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);3var browser = await Puppeteer.LaunchAsync(new LaunchOptions4{5});6var browserFetcher = new BrowserFetcher();7var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);8var browser = await Puppeteer.LaunchAsync(new LaunchOptions9{10});11var browserFetcher = new BrowserFetcher();12var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);13var browser = await Puppeteer.LaunchAsync(new LaunchOptions14{15});16var browserFetcher = new BrowserFetcher();17var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);18var browser = await Puppeteer.LaunchAsync(new LaunchOptions19{20});21var browserFetcher = new BrowserFetcher();22var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);23var browser = await Puppeteer.LaunchAsync(new LaunchOptions24{25});26var browserFetcher = new BrowserFetcher();27var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);28var browser = await Puppeteer.LaunchAsync(new LaunchOptions29{30});31var browserFetcher = new BrowserFetcher();32var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1var browserFetcher = new BrowserFetcher();2await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);3var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });4var browserFetcher = new BrowserFetcher();5await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);6var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });7var browserFetcher = new BrowserFetcher();8await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);9var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });10var browserFetcher = new BrowserFetcher();11await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);12var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });13var browserFetcher = new BrowserFetcher();14await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);15var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });16var browserFetcher = new BrowserFetcher();17await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);18var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });19var browserFetcher = new BrowserFetcher();20await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);21var browser = await Puppeteer.LaunchAsync(new LaunchOptions { ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision) });

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 }11 }12}

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 DownloadAsync().Wait();9 }10 static async Task DownloadAsync()11 {12 var browserFetcher = new BrowserFetcher();13 var revisionInfo = await browserFetcher.DownloadAsync(533271);14 var browser = await Puppeteer.LaunchAsync(new LaunchOptions15 {16 });17 var page = await browser.NewPageAsync();18 await page.ScreenshotAsync("google.png");19 await browser.CloseAsync();20 }21 }22}

Full Screen

Full Screen

DownloadAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browserFetcher = new BrowserFetcher();9 var revisionInfo = await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10 Console.WriteLine(revisionInfo.RevisionInfo.Local);11 }12 }13}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful