Best Puppeteer-sharp code snippet using PuppeteerSharp.BrowserFetcher.DownloadAsync
SaveWebPage.xaml.cs
Source:SaveWebPage.xaml.cs
...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}");
...
Form1.cs
Source:Form1.cs
...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);...
GistGithubService.cs
Source:GistGithubService.cs
...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}...
BrowserProvider.cs
Source:BrowserProvider.cs
...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}...
Browser.cs
Source:Browser.cs
...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 };
...
PuppeteerDocumentGenerator.cs
Source:PuppeteerDocumentGenerator.cs
...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...
PuppeteerConnection.cs
Source:PuppeteerConnection.cs
...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 */...
PuppeteerSharpConverterCollectionExtensions.cs
Source:PuppeteerSharpConverterCollectionExtensions.cs
...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}...
DownloadAsync
Using AI Code Generation
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);
DownloadAsync
Using AI Code Generation
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);
DownloadAsync
Using AI Code Generation
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) });
DownloadAsync
Using AI Code Generation
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}
DownloadAsync
Using AI Code Generation
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}
DownloadAsync
Using AI Code Generation
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}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!