Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.QuerySelectorAllAsync
VkBrwUserFactory.cs
Source:VkBrwUserFactory.cs
...49 {50 List<VkBrwCommunity> nwPages = new List<VkBrwCommunity>();51 await userPage.GoToAsync(user.ProfileLink);52 ElementHandle userIsLoaded = await userPage.WaitForSelectorAsync("#page_info_wrap.page_info_wrap");53 ElementHandle[] hiddenProfileBlock = await userPage.QuerySelectorAllAsync("h5.profile_deleted_text");54 if (hiddenProfileBlock.Length == 0)55 {56 ElementHandle idolsLoaded = await userPage.WaitForSelectorAsync("#profile_idols");57 // ÐнÑеÑеÑнÑе ÑÑÑаниÑÑ58 ElementHandle[] noteworthyPagesBlock = await userPage.QuerySelectorAllAsync("#profile_idols.module.clear.page_list_module._module");59 if (noteworthyPagesBlock.Length == 1)60 {61 ElementHandle noteworthyPages = noteworthyPagesBlock.First();62 ElementHandle[] noteworthyPagesHeaderBlock = await noteworthyPages.QuerySelectorAllAsync("a.module_header");63 if (noteworthyPagesHeaderBlock.Length == 1)64 {65 ClickOptions clickOptions = new ClickOptions { Delay = new Random().Next(30, 100) };66 ElementHandle noteworthyPagesLinkElement = noteworthyPagesHeaderBlock.First();67 await noteworthyPagesLinkElement.ClickAsync(clickOptions);68 ElementHandle noteworthyPagesIsOpened = await userPage.WaitForSelectorAsync("#fans_rowsidols.fans_rows.fans_idols");69 ElementHandle[] closeBlock = await userPage.QuerySelectorAllAsync("div.box_x_button");70 if (closeBlock.Length == 1)71 {72 ElementHandle[] pagesCountBlock = await userPage.QuerySelectorAllAsync("span.ui_box_header_cnt");73 if (pagesCountBlock.Length == 1)74 {75 ElementHandle pagesTotalCountElement = pagesCountBlock.First();76 string pagesTotalCountValue = await pagesTotalCountElement.EvaluateFunctionAsync<string>("('span', span => span.innerText)");77 if (!string.IsNullOrEmpty(pagesTotalCountValue))78 {79 bool pagesTotalCountReceived = int.TryParse(pagesTotalCountValue, out int pagesTotalCount);80 if (pagesTotalCountReceived && pagesTotalCount > 0)81 {82 ElementHandle[] pagesVisibleElements = await userPage.QuerySelectorAllAsync("div.fans_idol_row.inl_bl");83 if (pagesVisibleElements.Length < pagesTotalCount)84 {85 PressOptions pressOptions = new PressOptions { Delay = new Random().Next(20, 40)};86 await userPage.FocusAsync("input");87 await userPage.Keyboard.PressAsync("Tab", pressOptions);88 int visiblePagesCounter = pagesVisibleElements.Length;89 while (visiblePagesCounter < pagesTotalCount)90 {91 await userPage.Keyboard.PressAsync("PageDown", pressOptions);92 await Task.Delay(new Random().Next(250, 350));93 await userPage.Keyboard.PressAsync("PageDown", pressOptions);94 await Task.Delay(new Random().Next(250, 350));95 await userPage.Keyboard.PressAsync("PageDown", pressOptions);96 await Task.Delay(new Random().Next(250, 350));97 await userPage.Keyboard.PressAsync("PageDown", pressOptions);98 await Task.Delay(new Random().Next(250, 350));99 ElementHandle[] newPagesVisibleElements = await userPage.QuerySelectorAllAsync("div.fans_idol_row.inl_bl");100 if (newPagesVisibleElements.Length == visiblePagesCounter)101 {102 break;103 }104 visiblePagesCounter = newPagesVisibleElements.Length;105 }106 }107 ElementHandle[] nwPagesElements = await userPage.QuerySelectorAllAsync("div.fans_idol_info");108 foreach (var element in nwPagesElements)109 {110 VkBrwCommunity community = await GetCommunityAsync(element, userContext);111 if (community != null)112 {113 nwPages.Add(community);114 community.Users.Add(user);115 }116 }117 }118 }119 }120 ElementHandle closeButtonElement = closeBlock.First();121 await closeButtonElement.ClickAsync(clickOptions);122 }123 }124 }125 }126 else127 {128 user.HiddenProfile = true;129 ElementHandle[] pageNameElements = await userPage.QuerySelectorAllAsync("h2.page_name");130 if (pageNameElements.Length == 1)131 {132 var pageElement = pageNameElements.First();133 user.PageName = await pageElement.EvaluateFunctionAsync<string>("('h2', h2 => h2.innerText)");134 }135 }136 return nwPages;137 }138 private async Task<VkBrwCommunity> GetCommunityAsync(ElementHandle communityElement, BrowserContext userContext)139 {140 ElementHandle communityNameElement = await communityElement.QuerySelectorAsync("div.fans_idol_name");141 if (communityNameElement != null)142 {143 VkBrwCommunity nwPage = new VkBrwCommunity();...
Methods.cs
Source:Methods.cs
...29 string text, int timeBetweenKeystrokes = 0)30 {31 data.Logger.LogHeader();32 var frame = GetFrame(data);33 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];34 await elem.TypeAsync(text, new PuppeteerSharp.Input.TypeOptions { Delay = timeBetweenKeystrokes });35 data.Logger.Log($"Typed {text}", LogColors.DarkSalmon);36 }37 [Block("Types text in an input field with human-like random delays", name = "Type Human")]38 public static async Task PuppeteerTypeElementHuman(BotData data, FindElementBy findBy, string identifier, int index,39 string text)40 {41 data.Logger.LogHeader();42 var frame = GetFrame(data);43 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];44 foreach (var c in text)45 {46 await elem.TypeAsync(c.ToString());47 await Task.Delay(data.Random.Next(100, 300)); // Wait between 100 and 300 ms (average human type speed is 60 WPM ~ 360 CPM)48 }49 data.Logger.Log($"Typed {text}", LogColors.DarkSalmon);50 }51 [Block("Clicks an element", name = "Click")]52 public static async Task PuppeteerClick(BotData data, FindElementBy findBy, string identifier, int index,53 PuppeteerSharp.Input.MouseButton mouseButton = PuppeteerSharp.Input.MouseButton.Left, int clickCount = 1,54 int timeBetweenClicks = 0)55 {56 data.Logger.LogHeader();57 var frame = GetFrame(data);58 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];59 await elem.ClickAsync(new PuppeteerSharp.Input.ClickOptions { Button = mouseButton, ClickCount = clickCount, Delay = timeBetweenClicks });60 data.Logger.Log($"Clicked {clickCount} time(s) with {mouseButton} button", LogColors.DarkSalmon);61 }62 [Block("Submits a form", name = "Submit")]63 public static async Task PuppeteerSubmit(BotData data, FindElementBy findBy, string identifier, int index)64 {65 data.Logger.LogHeader();66 var elemScript = GetElementScript(findBy, identifier, index);67 var frame = GetFrame(data);68 var script = elemScript + ".submit();";69 await frame.EvaluateExpressionAsync(script);70 data.Logger.Log($"Submitted the form by executing {script}", LogColors.DarkSalmon);71 }72 [Block("Selects a value in a select element", name = "Select")]73 public static async Task PuppeteerSelect(BotData data, FindElementBy findBy, string identifier, int index, string value)74 {75 data.Logger.LogHeader();76 var frame = GetFrame(data);77 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];78 await elem.SelectAsync(value);79 data.Logger.Log($"Selected value {value}", LogColors.DarkSalmon);80 }81 [Block("Gets the value of an attribute of an element", name = "Get Attribute Value")]82 public static async Task<string> PuppeteerGetAttributeValue(BotData data, FindElementBy findBy, string identifier, int index,83 string attributeName = "innerText")84 {85 data.Logger.LogHeader();86 var elemScript = GetElementScript(findBy, identifier, index);87 var frame = GetFrame(data);88 var script = $"{elemScript}.{attributeName};";89 var value = await frame.EvaluateExpressionAsync<string>(script);90 data.Logger.Log($"Got value {value} of attribute {attributeName} by executing {script}", LogColors.DarkSalmon);91 return value;92 }93 [Block("Gets the values of an attribute of multiple elements", name = "Get Attribute Value All")]94 public static async Task<List<string>> PuppeteerGetAttributeValueAll(BotData data, FindElementBy findBy, string identifier,95 string attributeName = "innerText")96 {97 data.Logger.LogHeader();98 var elemScript = GetElementsScript(findBy, identifier);99 var frame = GetFrame(data);100 var script = $"Array.prototype.slice.call({elemScript}).map((item) => item.{attributeName})";101 var values = await frame.EvaluateExpressionAsync<string[]>(script);102 data.Logger.Log($"Got {values.Length} values for attribute {attributeName} by executing {script}", LogColors.DarkSalmon);103 return values.ToList();104 }105 [Block("Checks if an element is currently being displayed on the page", name = "Is Displayed")]106 public static async Task<bool> PuppeteerIsDisplayed(BotData data, FindElementBy findBy, string identifier, int index)107 {108 data.Logger.LogHeader();109 var elemScript = GetElementScript(findBy, identifier, index);110 var frame = GetFrame(data);111 var script = $"window.getComputedStyle({elemScript}).display !== 'none';";112 var displayed = await frame.EvaluateExpressionAsync<bool>(script);113 data.Logger.Log($"Found out the element is{(displayed ? "" : " not")} displayed by executing {script}", LogColors.DarkSalmon);114 return displayed;115 }116 [Block("Uploads a file to the selected element", name = "Upload File")]117 public static async Task PuppeteerUploadFile(BotData data, FindElementBy findBy, string identifier, int index, string filePath)118 {119 data.Logger.LogHeader();120 var frame = GetFrame(data);121 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];122 await elem.UploadFileAsync(filePath);123 data.Logger.Log($"Uploaded {filePath} to the element", LogColors.DarkSalmon);124 }125 [Block("Gets the X coordinate of the element in pixels", name = "Get Position X")]126 public static async Task<int> PuppeteerGetPositionX(BotData data, FindElementBy findBy, string identifier, int index)127 {128 data.Logger.LogHeader();129 var frame = GetFrame(data);130 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];131 var x = (int)(await elem.BoundingBoxAsync()).X;132 data.Logger.Log($"The X coordinate of the element is {x}", LogColors.DarkSalmon);133 return x;134 }135 [Block("Gets the Y coordinate of the element in pixels", name = "Get Position Y")]136 public static async Task<int> PuppeteerGetPositionY(BotData data, FindElementBy findBy, string identifier, int index)137 {138 data.Logger.LogHeader();139 var frame = GetFrame(data);140 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];141 var y = (int)(await elem.BoundingBoxAsync()).Y;142 data.Logger.Log($"The Y coordinate of the element is {y}", LogColors.DarkSalmon);143 return y;144 }145 [Block("Gets the width of the element in pixels", name = "Get Width")]146 public static async Task<int> PuppeteerGetWidth(BotData data, FindElementBy findBy, string identifier, int index)147 {148 data.Logger.LogHeader();149 var frame = GetFrame(data);150 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];151 var width = (int)(await elem.BoundingBoxAsync()).Width;152 data.Logger.Log($"The width of the element is {width}", LogColors.DarkSalmon);153 return width;154 }155 [Block("Gets the height of the element in pixels", name = "Get Height")]156 public static async Task<int> PuppeteerGetHeight(BotData data, FindElementBy findBy, string identifier, int index)157 {158 data.Logger.LogHeader();159 var frame = GetFrame(data);160 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];161 var height = (int)(await elem.BoundingBoxAsync()).Height;162 data.Logger.Log($"The height of the element is {height}", LogColors.DarkSalmon);163 return height;164 }165 [Block("Takes a screenshot of the element and saves it to an output file", name = "Screenshot Element")]166 public static async Task PuppeteerScreenshotElement(BotData data, FindElementBy findBy, string identifier, int index,167 string fileName, bool fullPage = false, bool omitBackground = false)168 {169 data.Logger.LogHeader();170 if (data.Providers.Security.RestrictBlocksToCWD)171 FileUtils.ThrowIfNotInCWD(fileName);172 var frame = GetFrame(data);173 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];174 await elem.ScreenshotAsync(fileName, new PuppeteerSharp.ScreenshotOptions { FullPage = fullPage, OmitBackground = omitBackground });175 data.Logger.Log($"Took a screenshot of the element and saved it to {fileName}", LogColors.DarkSalmon);176 }177 [Block("Takes a screenshot of the element and converts it to a base64 string", name = "Screenshot Element Base64")]178 public static async Task<string> PuppeteerScreenshotBase64(BotData data, FindElementBy findBy, string identifier, int index,179 bool fullPage = false, bool omitBackground = false)180 {181 data.Logger.LogHeader();182 var frame = GetFrame(data);183 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];184 var base64 = await elem.ScreenshotBase64Async(new PuppeteerSharp.ScreenshotOptions { FullPage = fullPage, OmitBackground = omitBackground });185 data.Logger.Log($"Took a screenshot of the element as base64", LogColors.DarkSalmon);186 return base64;187 }188 [Block("Switches to a different iframe", name = "Switch to Frame")]189 public static async Task PuppeteerSwitchToFrame(BotData data, FindElementBy findBy, string identifier, int index)190 {191 data.Logger.LogHeader();192 var frame = GetFrame(data);193 var elem = (await frame.QuerySelectorAllAsync(BuildSelector(findBy, identifier)))[index];194 data.Objects["puppeteerFrame"] = await elem.ContentFrameAsync();195 data.Logger.Log($"Switched to iframe", LogColors.DarkSalmon);196 }197 [Block("Waits for an element to appear on the page", name = "Wait for Element")]198 public static async Task PuppeteerWaitForElement(BotData data, FindElementBy findBy, string identifier, bool hidden = false, bool visible = true,199 int timeout = 30000)200 {201 data.Logger.LogHeader();202 var frame = GetFrame(data);203 await frame.WaitForSelectorAsync(BuildSelector(findBy, identifier),204 new PuppeteerSharp.WaitForSelectorOptions { Hidden = hidden, Visible = visible, Timeout = timeout });205 data.Logger.Log($"Waited for element with {findBy} {identifier}", LogColors.DarkSalmon);206 }207 private static string GetElementsScript(FindElementBy findBy, string identifier)...
PuppeteerBrowser.cs
Source:PuppeteerBrowser.cs
...70 ElementHandle buttonConfirm = null;71 int time = 0;72 while (buttonConfirm == null && ++time < 4)73 {74 var lstTemp = await MetaMaskPage.QuerySelectorAllAsync("button");75 var regexbtn = new Regex(@"^\s*Confirm\s*$", RegexOptions.IgnoreCase);76 foreach (var item in lstTemp)77 {78 var content = await MetaMaskPage.EvaluateFunctionAsync<string>("e => e.textContent", item);79 if (regexbtn.IsMatch(content))80 {81 buttonConfirm = item;82 break;83 }84 }85 await Task.Delay(3000);86 }87 if (buttonConfirm != null)88 {89 await buttonConfirm?.ClickAsync();90 }91 }92 public Page MetaMaskPage { get; set; }93 public async Task<bool> LoginAsync(string PassWord)94 {95 var pages = await _Browser.PagesAsync();96 RootPage = pages[0];97 await Task.Delay(5000);98 MetaMaskPage = await _Browser.NewPageAsync();99 await MetaMaskPage.BringToFrontAsync();100 await MetaMaskPage.GoToAsync(string.Format("chrome-extension://{0}/home.html#unlock", _Option.MetaMaskExtensionId));101 var pwd = await MetaMaskPage.WaitForSelectorAsync("*[type='password']");102 await pwd.TypeAsync(PassWord, new PuppeteerSharp.Input.TypeOptions { Delay = 100 });103 await Task.Delay(1000);104 var button = await MetaMaskPage.QuerySelectorAsync("button");105 await button.ClickAsync();106 var resCheck = MetaMaskPage.WaitForSelectorAsync("*[class='selected-account__clickable']");107 return resCheck != null;108 }109 public string _UrlGameApp = "https://dragonwars.game/#/train";110 public string _UrlMetaMashPopup { get => string.Format("chrome-extension://{0}/popup.html", _Option.MetaMaskExtensionId); }111 public string _UrlMetaMashHome { get => string.Format("chrome-extension://{0}/home.html#unlock", _Option.MetaMaskExtensionId); }112 public async Task InitialTabAsync()113 {114 var pages = await _Browser.PagesAsync();115 RootPage = pages[0];116 MetaMaskPage = await _Browser.NewPageAsync();117 }118 //public async Task<ElementHandle[]> GetHanleNavigateButtonsAsync()119 //{120 // await RootPage.WaitForSelectorAsync("ul.pagination li a");121 // return await RootPage.QuerySelectorAllAsync("ul.pagination li a");122 //}123 //public async Task<ElementHandle> GetHandleNavigateButtonAsync(string page)124 //{125 // var lstBtn = await GetHanleNavigateButtonsAsync();126 // return lstBtn.First(x => x.GetPropertyAsync("innerText").Result.JsonValueAsync().ToString().Equals(page));127 //}128 public async Task GotoNavigatePageAsync(int page)129 {130 await RootPage.BringToFrontAsync();131 await RootPage.GoToAsync($"https://dragonwars.game/#/train?page={page}");132 await Task.Delay(2000);133 }134 public async Task GotoNavigateMetaMashAsync()135 {136 await MetaMaskPage.BringToFrontAsync();137 await MetaMaskPage.GoToAsync(_UrlMetaMashPopup);138 await Task.Delay(2000);139 }140 public async Task<IList<PayloadDragonPage>> GetPageInfosAsync(MyPageDragonDTO myPageDragonDTO)141 {142 var lstData = new List<PayloadDragonPage>();143 for (int i = 1; i <= myPageDragonDTO.Payload.TotalPage; i++)144 {145 var client = new RestClient($"https://devabcde-api.dragonwars.game/v1/dragons/my-dragon?page={i}&rowPerPage=12&type=DRAGON");146 var request = new RestRequest();147 request.Method = Method.Get;148 request.AddHeader("wallet", _Option.Wallet);149 var response = await client.ExecuteAsync<MyPageDragonDTO>(request);150 lstData.Add(response.Data.Payload);151 }152 return lstData;153 }154 public async Task<IList<CardInfo>> GetCardItemsAsync(int page)155 {156 await Task.Delay(2000);157 await GotoNavigatePageAsync(page);158 var lstData = new List<CardInfo>();159 var lstTemp = await RootPage.QuerySelectorAllAsync(".grid > div.relative");160 var regexSpan = new Regex(@"^\s*#\d+\s*$");161 var regexbtn = new Regex(@"^\s*Adventure\s*$", RegexOptions.IgnoreCase);162 foreach (var item in lstTemp)163 {164 var spans = await item.QuerySelectorAllAsync("span");165 var card = new CardInfo();166 foreach (var span in spans)167 {168 var content = await span.EvaluateFunctionAsync<string>("e => e.textContent", item);169 if (regexSpan.IsMatch(content))170 {171 card.Id = content;172 break;173 }174 }175 //var buttons = await item.QuerySelectorAllAsync("button");176 //foreach (var button in buttons)177 //{178 // var content = await button.EvaluateFunctionAsync<string>("e => e.textContent", item);179 // if (regexbtn.IsMatch(content))180 // {181 // await item.ClickAsync();182 // card.AdventureButton = item;183 // break;184 // }185 //}186 lstData.Add(card);187 }188 return lstData;189 }...
Scraping.cs
Source:Scraping.cs
...20 var result = new List<(string url, string title)>();21 Console.WriteLine("Scanning areas:");22 var response = await page.GoToAsync(Program.BaseUrl + "/arcade");23 var dummy = await page.WaitForSelectorAsync("div.card a");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);...
TinEyeEngine.cs
Source:TinEyeEngine.cs
...36 await using Page page = await browser.NewPageAsync();37 await page.GoToAsync(BaseUrl + sd.UploadUri);38 await page.WaitForNavigationAsync();39 var rd = page.Url;40 var resultElems = await page.QuerySelectorAllAsync("div[class='match']");41 var img = new List<ImageResult>();42 var firstSpan = await (await resultElems[0].QuerySelectorAsync("span"))43 .GetPropertyAsync("textContent");44 if (firstSpan.ToValueString().Contains("sponsored", StringComparison.InvariantCultureIgnoreCase)) {45 resultElems = resultElems.Skip(1).ToArray();46 }47 foreach (ElementHandle elem in resultElems) {48 var ir = new ImageResult(r) { };49 var p = await elem.QuerySelectorAllAsync("p");50 var h4 = await elem.QuerySelectorAsync("h4");51 var name = await h4.GetPropertyAsync("textContent");52 ir.Name = name.ToValueString();53 foreach (ElementHandle t1 in p) {54 var a = await t1.QuerySelectorAllAsync("a");55 var uri = new List<Uri>();56 // a=a.Distinct().ToArray();57 foreach (ElementHandle t in a) {58 var href = await t.GetPropertyAsync("href");59 string s = href.ToValueString();60 if (!string.IsNullOrWhiteSpace(s)) {61 var item = new Uri(s);62 // item = item.Normalize();63 if (!uri.Contains(item)) {64 uri.Add(item);65 }66 }67 }68 ir.OtherUrl.AddRange(uri);69 var imgElems = await t1.QuerySelectorAllAsync("img");70 var imgList = new List<Uri>();71 for (int k = 0; k < imgElems.Length; k++) {72 var src = await a[k].GetPropertyAsync("src");73 imgList.Add(new Uri(src.ToValueString()));74 }75 ir.OtherUrl.AddRange(imgList);76 ir.Url = ir.OtherUrl.FirstOrDefault();77 }78 img.Add(ir);79 }80 browser.Dispose();81 return img;82 }83 protected override SearchResult Process(object obj, SearchResult sr)...
BuscaController.cs
Source:BuscaController.cs
...68 public class ObterDados69 {70 public async Task<string> GetLogradouro(Page page)71 { 72 var listREturn = await page.QuerySelectorAllAsync("td");73 string itemReturn = await listREturn[0].InnerTextAsync();74 if (itemReturn != null)75 {76 return itemReturn;77 }78 else79 {80 return "Não localizou";81 }82 }83 public async Task<string> GetBairro(Page page)84 {85 var listREturn = await page.QuerySelectorAllAsync("td");86 string itemReturn = await listREturn[1].InnerTextAsync();87 if (itemReturn != null)88 {89 return itemReturn;90 }91 else92 {93 return "Não localizou";94 }95 }96 public async Task<string> GetEstado(Page page)97 {98 var listREturn = await page.QuerySelectorAllAsync("td");99 string itemReturn = await listREturn[2].InnerTextAsync();100 if (itemReturn != null)101 {102 return itemReturn;103 }104 else105 {106 return "Não localizou";107 }108 }109 }110}...
InstagramScrapingUtils.cs
Source:InstagramScrapingUtils.cs
...14 var _commentSelector = ".C4VMK span";15 try16 {17 await LoadMoreComments(page);18 var _commentElements = await page.QuerySelectorAllAsync(_commentSelector);19 if (_commentElements != null)20 {21 foreach(var comment in _commentElements)22 {23 var _text = await comment.TextContentAsync();24 _comments.Add(_text);25 }26 }27 }28 catch (Exception ex)29 {30 Console.WriteLine(ex.ToString());31 throw ex;32 }33 return _comments;34 }35 public static async Task LoadMoreComments(Page page)36 {37 try38 {39 var _moreButtonSelector = "button.dCJp8";40 var _button = await page.WaitForSelectorAsync(_moreButtonSelector, new WaitForSelectorOptions() { Timeout = 3000 });41 if (_button != null)42 {43 await _button.ClickAsync();44 await LoadMoreComments(page);45 }46 }47 catch (Exception ex)48 {49 return;50 }51 }52 public static async Task<long> GetLikes(Page page)53 {54 long likes = default;55 try56 {57 var _likesSelector = "div.Nm9Fw button.sqdOP span";58 var _likesElement = await page.QuerySelectorAsync(_likesSelector);59 if (_likesElement != null)60 {61 var _text = await _likesElement.TextContentAsync();62 if (!string.IsNullOrEmpty(_text))63 likes = long.Parse(_text.Replace(",", ""));64 }65 }66 catch (Exception ex)67 {68 throw ex;69 }70 return likes;71 }72 public static async Task<List<string>> GetPhotoOrVideoUrls(Page page)73 {74 var _urls = new List<string>();75 try76 {77 var _selector = "div._97aPb img.FFVAD";78 var _elements = await page.QuerySelectorAllAsync(_selector);79 if (_elements != null)80 {81 foreach(var el in _elements)82 {83 var _url = await el.SrcAsync();84 _urls.Add(_url);85 }86 }87 }88 catch (Exception ex)89 {90 throw ex;91 }92 return _urls;...
GetRace.cs
Source:GetRace.cs
...21 Race race = new();22 List<Pilot> listPilot = new();23 TimeSpan time;24 await page.WaitForSelectorAsync(".vr-EventTimesNavBarButton");25 var tagTime = await page.QuerySelectorAllAsync(".vr-EventTimesNavBarButton");26 if (tagTime.Length == 0)27 return race;28 time = await tagTime[0].EvaluateFunctionAsync<TimeSpan>(@"el => el.textContent");29 if (Program.GetLatestTime() == time)30 return race;31 Program.SetLatestTime(time);32 Thread.Sleep(2000);33 var tagName = await page.QuerySelectorAllAsync(".vr-ParticipantVirtual_Identifier");34 if (tagName.Length == 0)35 return race;36 var tagOdds = await page.QuerySelectorAllAsync(".vr-ParticipantVirtualOddsOnly");37 if (tagOdds.Length == 0)38 return race;39 race.RaceDate = ValidateDate(time);40 for (int i = 0; i < tagOdds.Length; i++)41 {42 Pilot pilot = new()43 {44 PilotCode = i + 1,45 PilotName = await tagName[i].EvaluateFunctionAsync<string>(@"el => el.textContent"),46 Odd = await tagOdds[i].EvaluateFunctionAsync<double>(@"el => el.textContent")47 };48 listPilot.Add(pilot);49 }50 race.Pilots = listPilot;...
QuerySelectorAllAsync
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 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))10 using (var page = await browser.NewPageAsync())11 {12 var dimensions = await page.EvaluateFunctionAsync<PageDimensions>(@"() => {13 return {14 };15 }");16 Console.WriteLine("Dimensions: " + dimensions.Width + "x" + dimensions.Height);17 await page.ScreenshotAsync("screenshot.png");
QuerySelectorAllAsync
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 = true };9 using (var browser = await Puppeteer.LaunchAsync(options))10 {11 using (var page = await browser.NewPageAsync())12 {13 var elements = await page.QuerySelectorAllAsync("a");14 foreach (var element in elements)15 {16 Console.WriteLine(await element.GetAttributeAsync("href"));17 }18 }19 }20 }21 }22}
QuerySelectorAllAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 MainAsync().GetAwaiter().GetResult();10 }11 static async Task MainAsync()12 {13 var browser = await Puppeteer.LaunchAsync(new LaunchOptions14 {15 });16 var page = await browser.NewPageAsync();17 var elements = await page.QuerySelectorAllAsync("input");18 foreach (var element in elements)19 {20 Console.WriteLine(element);21 }22 await browser.CloseAsync();23 }24 }25}
QuerySelectorAllAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 await page.WaitForSelectorAsync("input[name=q]");14 await page.QuerySelectorAsync("input[name=q]").TypeAsync("puppeteer-sharp");15 await page.QuerySelectorAsync("input[name=q]").PressAsync("Enter");16 var links = await page.QuerySelectorAllAsync("h3.LC20lb");17 foreach (var link in links)18 {19 var text = await link.EvaluateFunctionAsync<string>("node => node.innerText");
QuerySelectorAllAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync();9 var page = await browser.NewPageAsync();10 var elements = await page.QuerySelectorAllAsync("a");11 Console.WriteLine("Count of elements: " + elements.Length);12 await browser.CloseAsync();13 }14 }15}
QuerySelectorAllAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 var elements = await page.QuerySelectorAllAsync("a");13 Console.WriteLine($"{elements.Length} elements found");14 await browser.CloseAsync();15 }16 }17}18How to select all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Page class?19How to select all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Frame class?20How to select all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.ElementHandle class?21How to select all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.JSHandle class?22How to get the outer HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Page class?23How to get the outer HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Frame class?24How to get the outer HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.ElementHandle class?25How to get the outer HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.JSHandle class?26How to get the inner HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Page class?27How to get the inner HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.Frame class?28How to get the inner HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.ElementHandle class?29How to get the inner HTML of all matching elements in a page using QuerySelectorAll() method of PuppeteerSharp.JSHandle class?
QuerySelectorAllAsync
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Threading.Tasks;5using PuppeteerSharp;6{7 {8 static void Main(string[] args)9 {10 var task = new Program().Run();11 task.Wait();12 }13 public async Task Run()14 {15 var browser = await Puppeteer.LaunchAsync(new LaunchOptions16 {17 });18 var page = await browser.NewPageAsync();19 var elements = await page.QuerySelectorAllAsync("div");20 Console.WriteLine($"Number of elements found: {elements.Count}");21 await browser.CloseAsync();22 }23 }24}25using System;26using System.Collections.Generic;27using System.Linq;28using System.Threading.Tasks;29using PuppeteerSharp;30{31 {32 static void Main(string[] args)33 {34 var task = new Program().Run();35 task.Wait();36 }37 public async Task Run()38 {39 var browser = await Puppeteer.LaunchAsync(new LaunchOptions40 {41 });42 var page = await browser.NewPageAsync();43 var elements = await page.QuerySelectorAllAsync("input");44 Console.WriteLine($"Number of elements found: {elements.Count}");45 await browser.CloseAsync();46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Threading.Tasks;53using PuppeteerSharp;54{55 {56 static void Main(string[] args)57 {
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!!