PuppeteerSharp是官方 Node.JSPuppeteerAPI 的.NET端口。
相关链接
API文档StackOverflowIssues用法截屏usingvarbrowserFetcher=newBrowserFetcher();awaitbrowserFetcher.DownloadAsync();awaitusingvarbrowser=awaitPuppeteer.LaunchAsync(newLaunchOptions{Headless=true});awaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.GoToAsync("https://www.google.com");awaitpage.ScreenshotAsync(outputFile);可以在生成屏幕截图之前更改视口
awaitPage.SetViewportAsync(newViewPortOptions{Width=500,Height=500});生成PDF文件usingvarbrowserFetcher=newBrowserFetcher();awaitbrowserFetcher.DownloadAsync();awaitusingvarbrowser=awaitPuppeteer.LaunchAsync(newLaunchOptions{Headless=true});awaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.GoToAsync("https://www.google.com");awaitpage.PdfAsync(outputFile);注入HTMLawaitusingvarpage=awaitbrowser.NewPageAsync();awaitpage.SetContentAsync("<div>MyReceipt</div>");varresult=awaitpage.GetContentAsync();评估JavaScriptawaitusingvarpage=awaitbrowser.NewPageAsync();varseven=awaitpage.EvaluateExpressionAsync<int>("4+3");varsomeObject=awaitpage.EvaluateFunctionAsync<dynamic>("(value)=>({a:value})",5);Console.WriteLine(someObject.a);等待选择器using(varpage=awaitbrowser.NewPageAsync()){awaitpage.GoToAsync("https://www.spapage.com");awaitpage.WaitForSelectorAsync("div.main-content")awaitpage.PdfAsync(outputFile));}等待函数using(varpage=awaitbrowser.NewPageAsync()){awaitpage.GoToAsync("https://www.spapage.com");varwatchDog=page.WaitForFunctionAsync("()=>window.innerWidth<100");awaitpage.SetViewportAsync(newViewPortOptions{Width=50,Height=50});awaitwatchDog;}连接到远程浏览器varoptions=newConnectOptions(){BrowserWSEndpoint=$"wss://www.externalbrowser.io?token={apikey}"};varurl="https://www.google.com/";using(varbrowser=awaitPuppeteerSharp.Puppeteer.ConnectAsync(options)){using(varpage=awaitbrowser.NewPageAsync()){awaitpage.GoToAsync(url);awaitpage.PdfAsync("wot.pdf");}}
评论