Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.QuerySelectorAsync
Scraping.cs
Source:Scraping.cs
...24 var linkList = await page.QuerySelectorAllAsync("div.card a");25 foreach(var link in linkList)26 {27 var url = await (await link.GetPropertyAsync("href")).JsonValueAsync<string>();28 var title = await(await(await link.QuerySelectorAsync("h4")).GetPropertyAsync("innerText")).JsonValueAsync<string>();29 Console.WriteLine($"Found area: {title}");30 result.Add((url: url, title: title));31 }32 return result;33 }34 public static async Task<List<TaskInfo>> ScanTasks(Page page, IEnumerable<(string url, string title)> areas)35 {36 var result = new List<TaskInfo>();37 foreach (var area in areas)38 {39 if (area.title == "Databases") continue; // Does not support SQL queries.40 if (area.title == "Python") continue; // Does not support multiple choice.41 Console.WriteLine($"Opening area {area.title}");42 var response = await page.GoToAsync(area.url);43 var dummy = await page.WaitForSelectorAsync("div.arcade-map--topic");44 Console.WriteLine($"Scanning area {area.title}");45 var topicList = await page.QuerySelectorAllAsync("div.arcade-map--topic");46 foreach (var topic in topicList)47 {48 var span = await topic.QuerySelectorAsync("span");49 var topicTitle = await (await span.GetPropertyAsync("innerText")).JsonValueAsync<string>();50 var taskList = await topic.QuerySelectorAllAsync("div.arcade-map--topic-details a.arcade-map--task");51 foreach (var task in taskList)52 {53 var taskUrl = await (await task.GetPropertyAsync("href")).JsonValueAsync<string>();54 var solvedText = await (await task.GetPropertyAsync("className")).JsonValueAsync<string>();55 var solved = !solvedText.Contains("-current") && !solvedText.Contains("-not-solved");56 var taskTitle = await (await (await task.QuerySelectorAsync("h3")).GetPropertyAsync("innerText")).JsonValueAsync<string>();57 result.Add(new TaskInfo(area.url, area.title, topicTitle, taskUrl, taskTitle, solved));58 }59 }60 }61 return result;62 }63 public static async Task RetrieveTask(Page page, TaskInfo task)64 {65 Console.Write($"Reading {task.Task} of {task.Topic}.");66 await page.GoToAsync(task.TaskUrl);67 Console.Write(".");68 await page.SetViewportAsync(new ViewPortOptions() { Height = 100000, Width = 8000, HasTouch = false, IsMobile = false });69 Console.Write(".");70 var source = await page.WaitForSelectorAsync("div.view-lines");71 Console.Write(".");72 task.Source = await (await source.GetPropertyAsync("innerText")).JsonValueAsync<string>();73 Console.Write(".");74 await page.SetViewportAsync(new ViewPortOptions() { Height = 800, Width = 1600, HasTouch = false, IsMobile = false });75 Console.Write(".");76 var desc = await page.QuerySelectorAsync("div.markdown");77 Console.Write(".");78 task.Description = await (await desc.GetPropertyAsync("innerText")).JsonValueAsync<string>();79 Console.Write(".");80 var index = task.Description.IndexOf("[C#] Syntax Tips");81 if (index > 0) task.Description = task.Description.Substring(0, index);82 if (Source.TestExists(task, out var _)) return; // No need to read tests.83 task.Tests = new List<string>();84 var list = await page.QuerySelectorAllAsync("div.accordion");85 Console.Write(".");86 foreach (var item in list)87 {88 var btn = await item.QuerySelectorAsync("div.accordion--head");89 Console.Write(".");90 PuppeteerSharp.ElementHandle body;91 do92 {93 await btn.ClickAsync();94 Console.Write(".");95 body = await item.QuerySelectorAsync("div.accordion--body");96 Console.Write(".");97 if (body == null) await Task.Delay(100);98 } while (body == null);99 string test;100 var warning = await body.QuerySelectorAsync("div.task-tests--warning");101 Console.Write(".");102 if (warning != null)103 {104 // The test example is too big, download it.105 var name = await (await btn.GetPropertyAsync("innerText")).JsonValueAsync<string>();106 Console.Write(".");107 name = name.Replace(' ', '-') + ".json";108 name = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", name);109 if (File.Exists(name)) File.Delete(name);110 var download = await warning.QuerySelectorAsync("div.button");111 Console.Write(".");112 await download.ClickAsync();113 Console.Write(".");114 int limit = 80;115 while (limit-- > 0 && !File.Exists(name))116 {117 await Task.Delay(250);118 }119 if (File.Exists(name))120 {121 test = ReadJsonTest(name);122 }123 else124 {125 test = "Failed to download test " + name;126 task.Failed = true;127 }128 }129 else130 {131 var input = await body.QuerySelectorAsync("pre.task-tests--value");132 if (input == null) break;133 test = await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();134 Console.Write(".");135 input = await body.QuerySelectorAsync("pre.-answer");136 Console.Write(".");137 test += "\nExpected Output: " + await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();138 Console.Write(".");139 }140 task.Tests.Add(test);141 }142 Console.WriteLine("");143 }144 private static string ReadJsonTest(string filename)145 {146 var jsonText = File.ReadAllText(filename);147 var json = JsonSerializer.Deserialize(jsonText, typeof(object)) as JsonElement?;148 StringBuilder result = new StringBuilder();149 json.Value.TryGetProperty("input", out var input);...
NavigateLogin.cs
Source:NavigateLogin.cs
...59 var response = await this.Page.GoToAsync(this.loginurl, new NavigationOptions60 {61 WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded },62 });63 this.elusername = await this.Page.QuerySelectorAsync("#user");64 this.elpassword = await this.Page.QuerySelectorAsync("input[name='password']");65 this.elbutton = await this.Page.QuerySelectorAsync("input.btn:nth-child(1)");66 if (this.elusername == null || this.elpassword == null || this.elbutton == null)67 {68 throw new Exception("NavigateLogin: Form input items missing.");69 }70 else71 {72 Console.WriteLine("Initialise Success.");73 }74 }75 private async Task ProcessAsync()76 {77 var usernameinput = await this.Page.QuerySelectorAsync("#user");78 await this.Page.EvaluateFunctionAsync($"yup => yup.value = '{this.Username}'", usernameinput);79 var passwordinput = await this.Page.QuerySelectorAsync("input[name='password']");80 await this.Page.EvaluateFunctionAsync($"mmhm => mmhm.value = '{this.Pasword}'", passwordinput);81 var form = await this.Page.QuerySelectorAsync("#loginForm");82 if (form == null)83 {84 throw new Exception("NavigateLogin: Page missing required login form.");85 }86 await this.Page.EvaluateFunctionAsync($"f => f.submit()", form);87 await this.Page.WaitForNavigationAsync(new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Load } });88 await this.Page.SetRequestInterceptionAsync(true);89 this.Page.Request += this.DenyRequests;90 }91 private async Task Commit()92 {93 // finalise operations94 if (this.Page.Url == "http://www.property-guru.co.nz/gurux/render.php?action=main")95 {...
AuthIntegrationTests.cs
Source:AuthIntegrationTests.cs
...35 {36 var page = await browser.NewPageAsync();37 await page.GoToAsync($"{app.RootUri}signup");38 {39 var emailField = await page.MainFrame.QuerySelectorAsync("#Email");40 await emailField.TypeAsync(Email);41 var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");42 await passwordField.TypeAsync(Password);43 var confirmPasswordField = await page.MainFrame.QuerySelectorAsync("#ConfirmPassword");44 await confirmPasswordField.TypeAsync(Password);45 var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");46 await submitButton.ClickAsync();47 await page.MainFrame.WaitForNavigationAsync();48 }49 {50 using var scope = app.Services.CreateScope();51 var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();52 var query = from user in dbContext.Users.AsQueryable() where user.Email == Email select user;53 var exists = await query.AnyAsync();54 exists.Should().Be(true);55 }56 {57 var cookies = await page.GetCookiesAsync();58 await page.DeleteCookieAsync(cookies);59 await page.GoToAsync($"{app.RootUri}login", waitUntil: WaitUntilNavigation.DOMContentLoaded);60 }61 {62 var emailField = await page.MainFrame.QuerySelectorAsync("#Email");63 await emailField.TypeAsync(Email);64 var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");65 await passwordField.TypeAsync(Password);66 var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");67 await submitButton.ClickAsync();68 await page.MainFrame.WaitForNavigationAsync();69 var url = new Uri(page.Url);70 url.AbsolutePath.Should().Be("/");71 }72 }73 }74}...
AvitoPostParser.cs
Source:AvitoPostParser.cs
...24 {25 ///todo try recognize captcha26 throw new HttpRequestException("Blocked");27 }28 var getPhoneNumberButton = await page.QuerySelectorAsync("span.item-phone-button-sub-text");29 if (getPhoneNumberButton == null)30 {31 return null;32 }33 await getPhoneNumberButton.ClickAsync();34 string phoneNumber = await GetPhoneNumber(page);35 string authorName = await GetAuthorName(page);36 return new PostInformation37 {38 Author = new Author39 {40 PhoneNumber = phoneNumber,41 Name = authorName42 }43 };44 }45 private async Task<string> GetAuthorName(PuppeteerSharp.Page page)46 {47 var sellerInfoName = await page.WaitForSelectorAsync(".seller-info-name.js-seller-info-name a").EvaluateFunctionAsync<string>("element => element.innerHTML");48 sellerInfoName = sellerInfoName.Trim();49 var sellerInfoValueElement = await page.QuerySelectorAsync("div.seller-info-label + div.seller-info-value");50 if (sellerInfoValueElement != null)51 {52 var sellerInfoValue = (await sellerInfoValueElement.EvaluateFunctionAsync<string>("element => element.innerHTML")).Trim();53 sellerInfoName += ": " + sellerInfoValue;54 }55 return sellerInfoName;56 }57 private async Task<string> GetPhoneNumber(PuppeteerSharp.Page page)58 {59 string phoneImage = await page.WaitForSelectorAsync("div[data-marker='phone-popup/content'] img[data-marker='phone-popup/phone-image']").EvaluateFunctionAsync<string>("element => element.src");60 string imageStr = phoneImage.Substring(phoneImage.IndexOf(",") + 1).Trim();61 byte[] imageByte = System.Convert.FromBase64String(imageStr);62 using (var engine = new TesseractEngine("./tessdata", "eng", EngineMode.Default))63 {...
Program.cs
Source:Program.cs
...33 // Height = 78034 //});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");56 var apaaoa = (await element.GetPropertyAsync("textContent")).JsonValueAsync() ;57 //var res = await element.EvaluateFunctionAsync<string>("textContent");5859 await browser.CloseAsync();606162 //var client = new SwedishBullsClient();63 ////var s = await client.GetSnapshot("8TRA.ST");64 ////Console.WriteLine(s.SignalUpdate);65 ////Console.WriteLine(s.MarketOutlook);6667 //await client.Login("test@gmail.com", "test");6869 }
...
LoginAwaiter.cs
Source:LoginAwaiter.cs
...16 protected override async Task RunAsync()17 {18 byte[] oldBase64QRCode = new byte[0];19 var startDate = DateTime.Now;20 var rememberMeCheckbox = await page.QuerySelectorAsync(REMEMBER_ME_CHECKBOK);21 if (rememberMeCheckbox is null)22 return;23 var isChecked = (bool)(await (await rememberMeCheckbox.GetPropertyAsync("checked")).JsonValueAsync());24 if (!isChecked)25 await rememberMeCheckbox.ClickAsync();26 do27 {28 var qrCodeImage = await page.QuerySelectorAsync(QR_CODE_IMAGE);29 if (qrCodeImage is null)30 {31 var refreshImage = await page.QuerySelectorAsync(REFRESH_IMAGE);32 if (!(refreshImage is null))33 await refreshImage.ClickAsync();34 else35 break;36 continue;37 }38 var srcQrCode = await qrCodeImage.ScreenshotDataAsync();39 bool isEqual = Enumerable.SequenceEqual(srcQrCode, oldBase64QRCode);40 oldBase64QRCode = srcQrCode;41 if (!isEqual)42 {43 OnQRCodeChange.Invoke(this, new QrCodeChangedArgs(srcQrCode));44 }45 }...
PuppeteerExtensions.cs
Source:PuppeteerExtensions.cs
...16 });17 }18 public static async Task TypeAsync(this Frame frame, string selector, string text)19 {20 await (await frame.QuerySelectorAsync(selector)).TypeAsync(text);21 }22 public static async Task ClickAsync(this Frame frame, string selector)23 {24 await (await frame.QuerySelectorAsync(selector)).ClickAsync();25 }26 public static async Task WaitForTruth(this Page page, string script, WaitForFunctionOptions opts = null)27 {28 var jsHandle = await page.WaitForExpressionAsync(script, opts);29 await jsHandle.DisposeAsync();30 }31 public static async Task WaitForDocumentInteractiveState(this Page page, int? timeout = null)32 {33 await page.WaitForTruth("document.readyState === 'interactive' || document.readyState === 'complete'", new WaitForFunctionOptions { Timeout = timeout ?? page.Browser.DefaultWaitForTimeout });34 }35 }36}...
LogoutAwaiter.cs
Source:LogoutAwaiter.cs
...15 {16 var startDate = DateTime.Now;17 do18 {19 var menuElement = await page.QuerySelectorAsync(MENU_IMAGE);20 if(!(menuElement is null))21 {22 await menuElement.ClickAsync();23 var logoutElement = await page.QuerySelectorAsync(LOGOUT_IMAGE);24 if (!(logoutElement is null))25 {26 await logoutElement.ClickAsync();27 break;28 }29 }30 }31 while (DateTime.Now.Subtract(startDate) < timeout);32 Thread.Sleep(1000);33 }34 }35}...
QuerySelectorAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 var element = await page.QuerySelectorAsync("input");12 await page.ScreenshotAsync("example.png");13 await browser.CloseAsync();14 }15 }16}17public Task<ElementHandle[]> QuerySelectorAllAsync(string selector)18using System;19using System.Threading.Tasks;20using PuppeteerSharp;21{22 {23 static async Task Main(string[] args)24 {25 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);26 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });27 var page = await browser.NewPageAsync();28 var elements = await page.QuerySelectorAllAsync("input");29 await page.ScreenshotAsync("example.png");30 await browser.CloseAsync();31 }32 }33}34public Task<ElementHandle> QuerySelectorXPathAsync(string selector)
QuerySelectorAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 var element = await page.QuerySelectorAsync("input[name='q']");12 Console.WriteLine(element);13 await browser.CloseAsync();14 }15 }16}
QuerySelectorAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;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 using (var page = await browser.NewPageAsync())11 {12 await page.QuerySelectorAsync("input[name='q']");13 }14 }15 }16}17using System;18using System.Threading.Tasks;19using PuppeteerSharp;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 using (var page = await browser.NewPageAsync())27 {28 var elements = await page.QuerySelectorAllAsync("input[name='q']");29 }30 }31 }32}33using System;34using System.Threading.Tasks;35using PuppeteerSharp;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 using (var page = await browser.NewPageAsync())43 {44 var element = await page.QuerySelectorAsync("input[name='q']");45 var value = await element.GetAttributeAsync("name");46 Console.WriteLine(value);47 }48 }49 }50}51using System;52using System.Threading.Tasks;53using PuppeteerSharp;
QuerySelectorAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;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 using (var page = await browser.NewPageAsync())12 {13 var elementHandle = await page.QuerySelectorAsync("input[name='q']");14 await elementHandle.TypeAsync("PuppeteerSharp");15 await page.Keyboard.PressAsync("Enter");16 await page.WaitForSelectorAsync("a[href='
QuerySelectorAsync
Using AI Code Generation
1var element = await page.QuerySelectorAsync("input[name='q']");2await element.TypeAsync("puppeteer");3await element.PressAsync("Enter");4await page.WaitForNavigationAsync();5var h3 = await page.QuerySelectorAsync("h3");6var title = await h3.EvaluateFunctionAsync<string>("node => node.innerText");7Console.WriteLine(title);8await page.WaitForSelectorAsync("a[href='
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!!