Best Puppeteer-sharp code snippet using PuppeteerSharp.Target
WebScraper.cs
Source:WebScraper.cs
1using System;2using System.Drawing;3using System.IO;4using System.Threading.Tasks;5using PuppeteerSharp;6using PuppeteerSharp.Input;7namespace ScraperionFramework8{9 /// <summary>10 /// Web scraper class.11 /// This class handles all interaction with chromium.12 /// </summary>13 public class WebScraper : IDisposable14 {15 private readonly Browser m_browser;16 private readonly Page m_page;17 private decimal m_MouseX = 0;18 private decimal m_MouseY = 0;19 /// <summary>20 /// Defualt agent string this library uses. Simulates Chrome installed on windows 10.21 /// </summary>22 public static readonly string DefaultAgent =23 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";24 /// <summary>25 /// Constructor.26 /// </summary>27 /// <param name="headless">Set to false to show chromium window.</param>28 /// <param name="agent">Agent to use when accessing pages. Uses DefaultAgent if non is set.</param>29 public WebScraper(bool headless = true, string agent = "")30 {31 if (agent == "")32 agent = DefaultAgent;33 var ops = new BrowserFetcherOptions34 {35 Path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\scraperion\\browser"36 };37 (new BrowserFetcher(ops).DownloadAsync(BrowserFetcher.DefaultRevision)).Wait();38 var browser = Puppeteer.LaunchAsync(new LaunchOptions39 {40 Headless = headless,41 IgnoreHTTPSErrors = true,42 });43 browser.Wait();44 m_browser = browser.Result;45 var page = m_browser.NewPageAsync();46 page.Wait();47 m_page = page.Result;48 m_page.Error += (s, e) => {49 Console.WriteLine("Error:" + e.ToString());50 };51 m_page.PageError += (s, e) =>52 {53 Console.WriteLine("Error:" + e.ToString());54 };55 m_page.Console += (s, e) => { Console.WriteLine(e.Message.Text); };56 m_page.SetUserAgentAsync(agent).Wait();57 }58 /// <summary>59 /// Set username and password to authenticate against web pages with.60 /// </summary>61 /// <param name="username">Username to authenticate with</param>62 /// <param name="password">Password to autrhenticate with.</param>63 public void SetAuth(string username, string password)64 {65 SetAuthAsync(username, password).Wait();66 }67 private async Task SetAuthAsync(string username, string password)68 {69 await m_page.AuthenticateAsync(new Credentials {Username = username, Password = password});70 }71 /// <summary>72 /// Sets the view port size of the page.73 /// </summary>74 /// <param name="width">Width of the page in pixels.</param>75 /// <param name="height">Height of page in pixels.</param>76 public void SetViewPort(int width, int height)77 {78 SetViewPortAsync(width, height).Wait();79 }80 private async Task SetViewPortAsync(int width, int height)81 {82 await m_page.SetViewportAsync(new ViewPortOptions83 {84 Width = width,85 Height = height86 });87 }88 /// <summary>89 /// Gets or sets the url the page is currently at.90 /// </summary>91 public string Url92 {93 get => m_page.Url;94 set95 {96 try97 {98 m_page.GoToAsync(value).Wait();99 }100 catch (Exception e)101 {102 Console.WriteLine(e);103 }104 }105 }106 /// <summary>107 /// Executes a javascript expression on page.108 /// This is simuilar to typing a command in the java console.109 /// </summary>110 /// <param name="script">Expression to run.</param>111 /// <returns>Json of executed result.</returns>112 public string Exec(string script)113 {114 var result = ExecAsync(script);115 result.Wait();116 return result.Result;117 }118 private async Task<string> ExecAsync(string script)119 {120 var data = await m_page.EvaluateExpressionAsync(script);121 return (string)data.ToString();122 }123 /// <summary>124 /// Takes a screenshot of the target page.125 /// </summary>126 /// <returns>Bitmap image containing screenshot.</returns>127 public Bitmap SnapshotBitmap()128 {129 var result = SnapshotBitmapAsync();130 result.Wait();131 return result.Result;132 }133 private async Task<Bitmap> SnapshotBitmapAsync()134 {135 var data = await m_page.ScreenshotStreamAsync();136 var image = new Bitmap(data);137 data.Dispose();138 return image;139 }140 /// <summary>141 /// Simulates key presses on page.142 /// </summary>143 /// <param name="text">Text to send to page.</param>144 public void SendKeys(string text)145 {146 SendKeysAsync(text).Wait();147 }148 private async Task SendKeysAsync(string text)149 {150 await m_page.Keyboard.TypeAsync(text);151 }152 /// <summary>153 /// Simulates moving the mouse on the page.154 ///155 /// Note: this does not move the system mouse.156 /// </summary>157 /// <param name="x">X coordinates to move mouse to.</param>158 /// <param name="y">Y coordinates to move mouse to.</param>159 public void MoveMouse(decimal x, decimal y)160 {161 MoveMouseAsync(x, y).Wait();162 }163 private async Task MoveMouseAsync(decimal x, decimal y)164 {165 await m_page.Mouse.MoveAsync(x, y);166 m_MouseX = x;167 m_MouseY = y;168 }169 /// <summary>170 /// Simulates a mouse click on page.171 /// </summary>172 /// <param name="button">Mouse button to simulate.</param>173 public void MouseClick(MouseButton button)174 {175 MouseClickAsync(button).Wait();176 }177 private async Task MouseClickAsync(MouseButton button)178 {179 await m_page.Mouse.ClickAsync(m_MouseX, m_MouseY, new ClickOptions{ Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });180 }181 /// <summary>182 /// Simulates a mouse up event on page.183 /// </summary>184 /// <param name="button">Mouse button to simulate.</param>185 public void MouseUp(MouseButton button)186 {187 MouseUpAsync(button).Wait();188 189 }190 private async Task MouseUpAsync(MouseButton button)191 {192 await m_page.Mouse.UpAsync(new ClickOptions { Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });193 }194 /// <summary>195 /// Simulates a mouse down event on page.196 /// </summary>197 /// <param name="button">Mouse button to simulate.</param>198 public void MouseDown(MouseButton button)199 {200 MouseDownAsync(button).Wait();201 }202 private async Task MouseDownAsync(MouseButton button)203 {204 await m_page.Mouse.DownAsync(new ClickOptions { Button = button == MouseButton.Left ? PuppeteerSharp.Input.MouseButton.Left : PuppeteerSharp.Input.MouseButton.Right });205 }206 /// <summary>207 /// Simulates a touch tap on a page.208 /// </summary>209 /// <param name="target">Javascript selector for element to tap on.</param>210 public void TapScreen(string target)211 {212 TapScreenAsync(target).Wait();213 }214 private async Task TapScreenAsync(string target)215 {216 await m_page.TapAsync(target);217 }218 /// <summary>219 /// Generates a pdf of the page.220 /// </summary>221 /// <returns>Stream containing the pdf data.</returns>222 public Stream CreatePdf()223 {224 var data = CreatePdfAsync();225 data.Wait();226 return data.Result;227 }228 private async Task<Stream> CreatePdfAsync()229 {230 return await m_page.PdfStreamAsync();231 }232 /// <summary>233 /// Waits for expression to be to be true.234 /// </summary>235 /// <param name="expression">Expression to wait on.</param>236 public void WaitOnScript(string expression)237 {238 WaitOnScriptAsync(expression).Wait();239 }240 private async Task WaitOnScriptAsync(string expression)241 {242 await m_page.WaitForExpressionAsync(expression);243 }244 /// <summary>245 /// Selects element on page to have focus.246 /// </summary>247 /// <param name="target">Javascript selector to make have focus.</param>248 public void Focus(string target)249 {250 FocusAsync(target).Wait();251 }252 private async Task FocusAsync(string target)253 {254 await m_page.FocusAsync(target);255 }256 /// <summary>257 /// Clicks on target element on page.258 /// </summary>259 /// <param name="target">Javascript selector of element to click on.</param>260 public void Click(string target)261 {262 ClickAsync(target).Wait();263 }264 private async Task ClickAsync(string target)265 {266 await m_page.ClickAsync(target);267 }268 /// <summary>269 /// Html content of page. Useful for scraping the html directly.270 /// </summary>271 public string Content272 {273 get274 {275 var data = m_page.GetContentAsync();276 data.Wait();277 return data.Result;278 }279 set280 {281 var data = m_page.SetContentAsync(value);282 data.Wait();283 }284 }285 /// <inheritdoc />286 /// <summary>287 /// Dispose method.288 /// This will close out chromium session.289 /// </summary>290 public void Dispose()291 {292 m_browser?.Dispose();293 m_page?.Dispose();294 }295 }296}...
Program.cs
Source:Program.cs
...69 private static LogFactory LoadNLogConfig()70 {71 try72 {73 var aspTargetEnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");74 var factory = NLogBuilder.ConfigureNLog(string.IsNullOrEmpty(aspTargetEnvironmentName) ? "nlog.config" : $"nlog.{aspTargetEnvironmentName}.config");75 return factory;76 }77 catch (FileNotFoundException ex)78 {79 Trace.WriteLine(ex);80 return NLogBuilder.ConfigureNLog("nlog.config");81 }82 }8384 public static IHostBuilder CreateHostBuilder(string[] args) =>85 Host.CreateDefaultBuilder(args)86 .ConfigureServices(s =>87 {88 //Startupã§åç
§ããçºã«è¿½å ã
...
GistGithubService.cs
Source:GistGithubService.cs
...95 private async Task<Credentials> FetchGitCredential(Uri redirectPage)96 {97 var targetUrl = await GetRedirectCallbackResult(redirectPage);98 var queryString = QueryStringHelper.ToDictionary(targetUrl);99 var code = queryString[gitHubModel.TargetRedirectKey!];100 var result = await client.Oauth.CreateAccessToken(101 new OauthTokenRequest(gitHubModel.ClientId, gitHubModel.ClientSecret, code));102 return new Credentials(result.AccessToken);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}...
PdfLibaryBenchMarks.cs
Source:PdfLibaryBenchMarks.cs
...21 _pdfGenerator = new PupeteerSharpPdfGen();22 _pdfGeneratorIron = new IronPdfGen();23 html = File.ReadAllText("Views/esrfoo.html");24 }25 [GlobalSetup(Targets = new[] { nameof(IronPdfSingle), nameof(IronPdfSequential), nameof(IronPdfParallel) })]26 public Task GlobalSetupA()27 {28 _pdfGeneratorIron.LaunchRenderer();29 Console.WriteLine("// " + "GlobalSetup Iron");30 return Task.CompletedTask;31 }32 [GlobalSetup(Targets = new[] { nameof(PupeteerPdfSingle), nameof(PupeteerPdfSequential), nameof(PupeteerPdfParallel) })]33 public async Task GlobalSetupB()34 {35 await _pdfGenerator.LaunchBrowserAsync(browserCount: 1);36 Console.WriteLine("// " + "GlobalSetup Pdf");37 }38 [Benchmark(Description = "PuppeteerSharp pdf 1 file")]39 public async Task PupeteerPdfSingle()40 {41 var result = await _pdfGenerator.GeneratePdf(html, "./pdfs/puppeteer.pdf");42 Console.WriteLine("bytes: " + result?.Length, ConsoleColor.Green);43 }44 [Benchmark(Description = "IronPdf pdf 1 files")]45 public async Task IronPdfSingle()46 {...
ChromiumHelper.cs
Source:ChromiumHelper.cs
...81 }82 return null;83 } 84 85 public void SetTargetChangedHandler(EventHandler<TargetChangedArgs> eventHandler)86 {87 if(browser != null)88 {89 browser.TargetChanged += eventHandler;90 }91 }92 public void RemoveTargetChangedHandler(EventHandler<TargetChangedArgs> eventHandler)93 {94 if (browser != null)95 {96 browser.TargetChanged -= eventHandler;97 }98 }99 public async Task<string> GetHtmlSource()100 {101 if(page != null)102 {103 return await page.GetContentAsync();104 }105 return "";106 }107 public async Task<string> GetHtmlSource(string url)108 {109 await LaunchUrl(url);110 return await GetHtmlSource();...
IPuppeteerBrowser.cs
Source:IPuppeteerBrowser.cs
1using System;2using System.Diagnostics;3using System.Threading.Tasks;4using ItWasMe.PuppeteerBrowserBasics.Models;5using PuppeteerSharp;6using PuppeteerSharp.Input;7namespace ItWasMe.PuppeteerBrowserBasics8{9 public interface IPuppeteerBrowser : IDisposable10 {11 Task ScrollPageSync(Page page, int maxScroll = 40000, int step = 200);12 Task ScrollPageSync(Page page, TimeSpan stepDelay, int maxScroll = 40000, int step = 200);13 Task<string> GetPageContent(string stringUrl, params WaitUntilNavigation[] waitUntilNavigations);14 Task<Page> GetPage(string url, TimeSpan? timeoutPageLoad, params WaitUntilNavigation[] waitUntilNavigations);15 Task<Page[]> GetPages();16 Task<Page> WaitGetPage(Page page, string selector, string targetId);17 Task<Page> ClickAndGetPage(ElementHandle element, string targetId);18 Task<Page> ClickAndGetPage(ElementHandle element, string targetId, WaitForOptions waitForOptions);19 Task<string> GetContentAsync(Page page);20 Task ClickAsync(Page page, string clickPlace, ClickOptions clickOptions = null);21 Task<string> GetFramePage(string iframeSrc);22 Task<string> GetFramePage(Page page, string frameName);23 Task<string> GetVideoAdsOnUrl(Page page, string xPath);24 Task<string> GetScrolledPage(string videoUrl, int maxScroll, params WaitUntilNavigation[] waitUntilNavigations);25 Task<string> GetPagePermission(Page page, string permissionName);26 Task<string> ClickButtonForContinue(Page page, string buttonXPath, string adUrl,27 params WaitUntilNavigation[] waitUntilNavigations);28 /// <summary>29 /// Returns IpDetails by the specified url30 /// </summary>31 /// <param name="ipFetcherUrl">The ip details provider url.</param>32 /// <returns>The instance of <see cref="IpDetails"></see></returns>33 Task<IpDetails> GetIpDetails(string ipFetcherUrl);34 /// <summary>35 /// Returns IpDetails by the specified url and, in case of error, returns the default country code and country name36 /// </summary>37 /// <param name="ipFetcherUrl">The ip details provider url.</param>38 /// <param name="defaultCountryCode">The country code tu return default details.</param>39 /// <returns>The instance of <see cref="IpDetails"></see></returns>40 Task<IpDetails> GetIpDetails(string ipFetcherUrl, string defaultCountryCode);41 Task CloseAsync();42 Task<Page> MouseClickAndGetPage(BoundingBox box, Page page);43 }44}...
ServiceWorkerDetails.cs
Source:ServiceWorkerDetails.cs
...6namespace PWABuilder.ServiceWorkerDetector.Models7{8 public class ServiceWorkerDetails9 {10 public ServiceWorkerDetails(Target target, Uri uri)11 {12 this.Target = target;13 this.Uri = uri;14 }15 public Target Target { get; }16 public Uri Uri { get; }17 }18}...
BaseParseTarget.cs
Source:BaseParseTarget.cs
1using System.Threading.Tasks;2using PuppeteerSharp;3namespace WebParser.Parser.Targets4{5 public abstract class BaseParseTarget : IParseTarget6 {7 protected BaseParseTarget(string url)8 {9 Url = url;10 }11 public string Url { get; set; }12 public abstract Task<bool> Parse(Page page);13 }14}...
Target
Using AI Code Generation
1{2 static void Main(string[] args)3 {4 Task.Run(async () =>5 {6 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);7 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions8 {9 Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox" }10 }))11 using (var page = await browser.NewPageAsync())12 {13 await page.ScreenshotAsync("1.png");14 }15 }).Wait();16 }17}
Target
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 var browser = await Puppeteer.LaunchAsync(new LaunchOptions13 {14 {15 }16 });17 var page = await browser.NewPageAsync();18 await page.ScreenshotAsync("google.png");19 await browser.CloseAsync();20 }21 }22}23using PuppeteerSharp;24using System;25using System.Threading.Tasks;26{27 {28 static void Main(string[] args)29 {30 MainAsync().Wait();31 }32 static async Task MainAsync()33 {34 var browser = await Puppeteer.LaunchAsync(new LaunchOptions35 {36 {37 }38 });39 var page = await browser.NewPageAsync();40 await page.ScreenshotAsync("google.png");41 await browser.CloseAsync();42 }43 }44}45using PuppeteerSharp;46using System;47using System.Threading.Tasks;48{49 {50 static void Main(string[] args)51 {52 MainAsync().Wait();53 }54 static async Task MainAsync()55 {56 var browser = await Puppeteer.LaunchAsync(new LaunchOptions57 {
Target
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var options = new LaunchOptions { Headless = false };9 using (var browser = await Puppeteer.LaunchAsync(options))10 {11 var page = await browser.NewPageAsync();12 await page.ScreenshotAsync("google.png");13 }14 }15 }16}17using PuppeteerSharp;18using System;19using System.Threading.Tasks;20{21 {22 static async Task Main(string[] args)23 {24 var options = new LaunchOptions { Headless = false };25 using (var browser = await Puppeteer.LaunchAsync(options))26 {27 var page = await browser.NewPageAsync();28 await page.ScreenshotAsync("google.png");29 }30 }31 }32}33using PuppeteerSharp;34using System;35using System.Threading.Tasks;36{37 {38 static async Task Main(string[] args)39 {40 var options = new LaunchOptions { Headless = false };41 using (var browser = await Puppeteer.LaunchAsync(options))42 {43 var page = await browser.NewPageAsync();44 await page.ScreenshotAsync("google.png");45 }46 }47 }48}49using PuppeteerSharp;50using System;51using System.Threading.Tasks;52{53 {54 static async Task Main(string[] args)55 {56 var options = new LaunchOptions { Headless = false };57 using (var browser = await Puppeteer.LaunchAsync(options))58 {59 var page = await browser.NewPageAsync();60 await page.ScreenshotAsync("google.png");61 }62 }63 }64}
Target
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 Console.WriteLine("Press any key to continue...");10 Console.ReadKey();11 MainAsync().Wait();12 }13 static async Task MainAsync()14 {15 var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";16 {17 };18 using (var browser = await Puppeteer.LaunchAsync(options))19 using (var page = await browser.NewPageAsync())20 {21 Console.WriteLine("Press any key to continue...");22 Console.ReadKey();23 await page.ScreenshotAsync("google.png");24 }25 }26 }27}28using PuppeteerSharp;29using System;30using System.Threading.Tasks;31{32 {33 static void Main(string[] args)34 {35 Console.WriteLine("Hello World!");36 Console.WriteLine("Press any key to continue...");37 Console.ReadKey();38 MainAsync().Wait();39 }40 static async Task MainAsync()41 {42 var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";43 {44 };45 using (var browser = await Puppeteer.LaunchAsync(options))46 using (var page = await browser.NewPageAsync())47 {48 Console.WriteLine("Press any key to continue...");49 Console.ReadKey();50 await page.ScreenshotAsync("google.png");51 }52 }53 }54}55using PuppeteerSharp;56using System;57using System.Threading.Tasks;58{59 {60 static void Main(string[] args)61 {62 Console.WriteLine("Hello World!");63 Console.WriteLine("Press any key to continue...");64 Console.ReadKey();
Target
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 public Target()7 {8 }9 public string Url { get; set; }10 public string Type { get; set; }11 public string Title { get; set; }12 public string Description { get; set; }13 public string FaviconUrl { get; set; }14 }15}16using PuppeteerSharp;17{18 {19 public Target()20 {21 }22 public string Url { get; set; }23 public string Type { get; set; }24 public string Title { get; set; }25 public string Description { get; set; }26 public string FaviconUrl { get; set; }27 }28}29using PuppeteerSharp;30{31 {32 public Target()33 {34 }35 public string Url { get; set; }36 public string Type { get; set; }37 public string Title { get; set; }38 public string Description { get; set; }39 public string FaviconUrl { get; set; }40 }41}42using PuppeteerSharp;43{44 {45 public Target()46 {47 }48 public string Url { get; set; }49 public string Type { get; set; }50 public string Title { get; set; }51 public string Description { get; set; }52 public string FaviconUrl { get; set; }53 }54}55using PuppeteerSharp;56{57 {58 public Target()59 {60 }61 public string Url { get; set; }62 public string Type { get; set; }63 public string Title { get; set; }64 public string Description { get; set; }65 public string FaviconUrl { get; set; }66 }67}
Target
Using AI Code Generation
1using PuppeteerSharp;2{3 {4 static void Main(string[] args)5 {6 var browser = new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;7 var browser = Puppeteer.LaunchAsync(new LaunchOptions8 {9 Args = new string[] { "--start-maximized" }10 }).Result;11 var page = browser.NewPageAsync().Result;12 page.ScreenshotAsync("screenshot.png").Wait();13 browser.CloseAsync().Wait();14 }15 }16}17using PuppeteerSharp;18{19 {20 static void Main(string[] args)21 {22 var browser = new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;23 var browser = Puppeteer.LaunchAsync(new LaunchOptions24 {25 Args = new string[] { "--start-maximized" }26 }).Result;27 var page = browser.NewPageAsync().Result;28 page.ScreenshotAsync("screenshot.png").Wait();29 browser.CloseAsync().Wait();30 }31 }32}33using PuppeteerSharp;34{35 {36 static void Main(string[] args)37 {38 var browser = new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;39 var browser = Puppeteer.LaunchAsync(new LaunchOptions40 {41 Args = new string[] { "--start-maximized" }42 }).Result;43 var page = browser.NewPageAsync().Result;44 page.GoToAsync("
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!!