Best Puppeteer-sharp code snippet using PuppeteerSharp.ElementHandle.XPathAsync
Program.cs
Source:Program.cs
...108109 public static async Task<string[]> GetAllCurrencies(Page page)110 {111 await page.WaitForSelectorAsync("#pjname");112 var elementHandles = await page.XPathAsync($"//select[@id=\"pjname\"]/option[position()>1]");113 //var all_currencies = await page.EvaluateExpressionAsync<string[]>("Array.from(document.querySelectorAll('option')).map(option => option.value);");114 if (elementHandles.Length == 0)115 {116 throw new Exception("No currencies available");117 }118 var all_currencies = await ElementHandlesToArrayOfStrings(elementHandles, "value");119 return all_currencies;120 }121122 public static async Task<string[]> ElementHandlesToArrayOfStrings(ElementHandle[] elementHandles, string property)123 {124 string[] return_value = new string[elementHandles.Length];125 int i = 0;126 foreach (var data in elementHandles)127 {128 var jsHandle = await data.GetPropertyAsync(property);129 string data_value = await jsHandle.JsonValueAsync<string>();130 return_value[i++] = data_value;131 }132133 return return_value;134 }135136 public static async Task<bool> SearchDataForCurrency(Page page, string url, string date1, string date2, string currency)137 {138 //await page.GoToAsync(url);139 await page.WaitForSelectorAsync("input[type='text'][name='erectDate']");140 await page.ClickAsync("input[type='text'][name='erectDate']", new PuppeteerSharp.Input.ClickOptions { ClickCount = 3 });141 await page.TypeAsync("input[type='text'][name='erectDate']", date1);142143 await page.WaitForSelectorAsync("input[type='text'][name='nothing']");144 await page.ClickAsync("input[type = 'text'][name = 'nothing']", new PuppeteerSharp.Input.ClickOptions { ClickCount = 3 });145 await page.TypeAsync("input[type='text'][name='nothing']", date2);146147 await page.WaitForSelectorAsync("#pjname");148 await page.SelectAsync("#pjname", currency);149150 await page.WaitForSelectorAsync("input[type='button'][value='search']");151 await page.ClickAsync("input[type='button'][value='search']");152153 bool data_exist = await WaitForSearchingToComplete(page);154 return data_exist;155 }156157 public static async Task<bool> WaitForSearchingToComplete(Page page)158 {159 bool data_exist = false;160 bool loaded = false;161 while (!loaded)162 {163 await page.WaitForSelectorAsync(".hui12_20");164 var elementHandles = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"soryy,wrong search word submit,please check your search word!\"]");165 var search_failed_msg = await ElementHandlesToArrayOfStrings(elementHandles, "innerHTML");166 if (search_failed_msg.Length == 0)167 {168 loaded = true;169 await page.WaitForSelectorAsync(".hui12_20");170 var elementHandles2 = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"sorry, no recordsï¼\"]");171 var no_records_msg = await ElementHandlesToArrayOfStrings(elementHandles2, "innerHTML");172 if (no_records_msg.Length == 0)173 {174 data_exist = true;175 }176 }177 else178 {179 Console.WriteLine("Website isn't behaving as expected, this may take a while...");180 await page.WaitForSelectorAsync("input[type='button'][value='search']");181 await page.ClickAsync("input[type='button'][value='search']");182 }183 }184 return data_exist;185 }186187 public static async Task<string[]> GetCurrencyDataHeader(Page page)188 {189 var elementHandles = await page.XPathAsync($"//td[@class=\"lan12_hover\"][text()]");190 var header = await ElementHandlesToArrayOfStrings(elementHandles, "innerHTML");191 return header;192 }193194 public static async Task<string[]> GetCurrencyData(Page page)195 {196 if(SEARCH_ALL_PAGES)197 {198 //get page number199 var elementHandles_number_of_pages = await page.XPathAsync($"//span[@class=\"nav_pagenum\"]");200 var pagenum_string_array = await ElementHandlesToArrayOfStrings(elementHandles_number_of_pages, "innerHTML");201 int number_of_pages = 1;202 if (pagenum_string_array.Length != 0)203 {204 number_of_pages = Int32.Parse(pagenum_string_array[0]);205 }206 else207 {208 throw new Exception("Could not find number of pages for current currency");209 }210211 //get data from every page212 List<string> records = new List<string>();213 for (int curr_page = 1; curr_page <= number_of_pages; ++curr_page)214 {215 //take data from current page216 Console.WriteLine("Loading data from page: " + curr_page + "...");217 var elementHandles_records = await page.XPathAsync($"//td[@class=\"hui12_20\"]");218 records.AddRange(await ElementHandlesToArrayOfStrings(elementHandles_records, "innerHTML"));219 Console.WriteLine("Data from page: " + curr_page + " loaded");220221 if (curr_page == number_of_pages) break;222 //else go to next page223 bool new_page_loaded_successfully = false;224 while (!new_page_loaded_successfully)225 {226 //go to next page227 await page.WaitForSelectorAsync("span[class='wcm_pointer nav_go_next']");228 await page.ClickAsync("span[class='wcm_pointer nav_go_next']");229230 //check if next page loaded successfully231 await page.WaitForSelectorAsync(".hui12_20");232 var elementHandles_search_error_msg = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"soryy,wrong search word submit,please check your search word!\"]");233 var search_error_msg = await ElementHandlesToArrayOfStrings(elementHandles_search_error_msg, "innerHTML");234 if (search_error_msg.Length != 0)235 {236 Console.WriteLine("Website isn't behaving as expected, this may take a while...");237 //if not, recover to current page238 bool loaded = false;239 while (!loaded)240 {241 //recovery process:242 await page.WaitForSelectorAsync(".hui12_20");243 var elementHandles = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"soryy,wrong search word submit,please check your search word!\"]");244 var search_failed_msg = await ElementHandlesToArrayOfStrings(elementHandles, "innerHTML");245 if (search_failed_msg.Length != 0)246 {247 await page.WaitForSelectorAsync("input[type='button'][value='search']");248 await page.ClickAsync("input[type='button'][value='search']");249 }250 else251 {252 await page.WaitForSelectorAsync(".nav_page");253 var elementHandles_nav_pages_links = await page.XPathAsync($"//span[@class=\"nav_page\"]/a");254 var nav_pages_links = await ElementHandlesToArrayOfStrings(elementHandles_nav_pages_links, "innerHTML");255256 bool recovery_start_page_success = true;257 int recovery_start_page_number = 1;258 foreach(var link in nav_pages_links)259 {260 int link_pagenum = Int32.Parse(link);261 if (link_pagenum <= curr_page && link_pagenum > recovery_start_page_number)262 {263 recovery_start_page_number = link_pagenum;264 }265 }266 if(recovery_start_page_number != 1)267 {268 await page.WaitForSelectorAsync("span[class='nav_page']");269 await page.ClickAsync("span[class='nav_page'] a[onclick~='(" + recovery_start_page_number + "," + number_of_pages + ")']");270271 await page.WaitForSelectorAsync(".hui12_20");272 var elementHandles_search_error_msg3 = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"soryy,wrong search word submit,please check your search word!\"]");273 var search_error_msg3 = await ElementHandlesToArrayOfStrings(elementHandles_search_error_msg3, "innerHTML");274 if (search_error_msg3.Length != 0)275 {276 recovery_start_page_success = false;277 }278 }279 if(recovery_start_page_success)280 {281 int i;282 for (i = recovery_start_page_number; i < curr_page; ++i)283 {284 await page.WaitForSelectorAsync("span[class='wcm_pointer nav_go_next']");285 await page.ClickAsync("span[class='wcm_pointer nav_go_next']");286287 await page.WaitForSelectorAsync(".hui12_20");288 var elementHandles_search_error_msg2 = await page.XPathAsync($"//td[@class=\"hui12_20\"][text()=\"soryy,wrong search word submit,please check your search word!\"]");289 var search_error_msg2 = await ElementHandlesToArrayOfStrings(elementHandles_search_error_msg2, "innerHTML");290 if (search_error_msg2.Length != 0)291 {292 break;293 }294 }295 if (i == curr_page)296 {297 loaded = true;298 }299 }300 }301 }302 }303 else304 {305 //if it is loaded successfully, just continue306 new_page_loaded_successfully = true;307 }308 }309 310 }311312 return records.ToArray();313 }314 else315 {316 //get element from only first page317 var elementHandles_records = await page.XPathAsync($"//td[@class=\"hui12_20\"]");318 var records = await ElementHandlesToArrayOfStrings(elementHandles_records, "innerHTML");319 return records;320 }321 322 }323324 public static async Task WriteDataToOutputFile(string filepath, string[] header, string[] records)325 {326 using (System.IO.StreamWriter file = new System.IO.StreamWriter(@filepath, false))327 {328 int number_of_columns = 7;329 //write header330 string line = "";331 for (int i = 0; i < number_of_columns; ++i)
...
InvocationExtensions.cs
Source:InvocationExtensions.cs
...96 var page = pageObject.Page;97 if (page == null) return null;98 if (invocation.IsReturning<ElementHandle[]>())99 {100 return await page.XPathAsync(attribute.Expression).ConfigureAwait(false);101 }102 if (invocation.IsReturningElementObjectArray())103 {104 var arrayType = invocation.Method.ReturnType.GetGenericArguments()[0];105 var proxyType = arrayType.GetElementType();106 var elementHandles = await page.XPathAsync(attribute.Expression).ConfigureAwait(false);107 return ProxyFactory.ElementObjectArray(proxyType, page, elementHandles);108 }109 return null;110 }111 public static async Task<object> GetReturnValueAsync(this IInvocation invocation, ElementObject elementObject, XPathAttribute attribute)112 {113 var element = elementObject.Element;114 if (element == null) return null;115 if (invocation.IsReturning<ElementHandle[]>())116 {117 return await element.XPathAsync(attribute.Expression).ConfigureAwait(false);118 }119 if (invocation.IsReturningElementObjectArray())120 {121 var arrayType = invocation.Method.ReturnType.GetGenericArguments()[0];122 var proxyType = arrayType.GetElementType();123 var elementHandles = await element.XPathAsync(attribute.Expression).ConfigureAwait(false);124 return ProxyFactory.ElementObjectArray(proxyType, elementObject.Page, elementHandles);125 }126 return null;127 }128 }129}...
ShiftSearchClient.cs
Source:ShiftSearchClient.cs
...85 }86 }87 protected async Task<ElementHandle?> GetElement(Page page, string xPathMatch)88 {89 ElementHandle[] elementHandles = await page.XPathAsync(xPathMatch);90 List<ElementHandle> visibleHandles = new List<ElementHandle>();91 foreach (ElementHandle handle in elementHandles)92 {93 if (await handle.IsIntersectingViewportAsync())94 {95 visibleHandles.Add(handle);96 }97 }98 if (visibleHandles.Count == 0)99 {100 return null;101 }102 if (visibleHandles.Count > 1)103 {...
RagingBullClient.cs
Source:RagingBullClient.cs
...94 return await GetElement(page, xPathMatch);95 }96 protected async Task<ElementHandle?> GetElement(Page page, string xPathMatch)97 {98 ElementHandle[] elementHandles = await page.XPathAsync(xPathMatch);99 List<ElementHandle> visibleHandles = new List<ElementHandle>();100 foreach (ElementHandle handle in elementHandles)101 {102 if (await handle.IsIntersectingViewportAsync())103 {104 visibleHandles.Add(handle);105 }106 }107 if (visibleHandles.Count == 0)108 {109 return null;110 }111 if (visibleHandles.Count > 1)112 {...
ElementHandleExtensions.cs
Source:ElementHandleExtensions.cs
...45 /// <typeparam name="T">The type of <see cref="ElementObject" /></typeparam>46 /// <param name="elementHandle">A <see cref="ElementHandle" /></param>47 /// <param name="expression">Expression to evaluate <see href="https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate" /></param>48 /// <returns>Task which resolves to the <see cref="ElementObject" /> array</returns>49 /// <seealso cref="ElementHandle.XPathAsync(string)"/>50 public static async Task<T[]> XPathAsync<T>(this ElementHandle elementHandle, string expression) where T : ElementObject51 {52 var results = await elementHandle.GuardFromNull().XPathAsync(expression).ConfigureAwait(false);53 return results.Select(x => ProxyFactory.ElementObject<T>(x.GetPage(), x)).ToArray();54 }55 private static Page GetPage(this ElementHandle elementHandle)56 {57 var propertyInfo = elementHandle.GetType().GetProperty("Page", BindingFlags.NonPublic | BindingFlags.Instance);58 var methodInfo = propertyInfo?.GetGetMethod(nonPublic: true);59 return methodInfo?.Invoke(elementHandle, null) as Page;60 }61 private static ElementHandle GuardFromNull(this ElementHandle elementHandle)62 {63 if (elementHandle == null) throw new ArgumentNullException(nameof(elementHandle));64 return elementHandle;65 }66 }...
ElementHandleExtensionsTests.cs
Source:ElementHandleExtensionsTests.cs
...32 result = await _elementHandle.QuerySelectorAsync<FakeElementObject>(".missing");33 Assert.Null(result);34 }35 [Fact]36 public async Task XPathAsync_returns_proxies_of_type()37 {38 var result = await _elementHandle.XPathAsync<FakeElementObject>("//div");39 Assert.NotEmpty(result);40 Assert.All(result, x => Assert.IsAssignableFrom<FakeElementObject>(x));41 Assert.All(result, x => Assert.NotNull(x.Page));42 result = await _elementHandle.XPathAsync<FakeElementObject>("//missing");43 Assert.Empty(result);44 }45 }46}...
ElementHandleXPathTests.cs
Source:ElementHandleXPathTests.cs
...17 {18 await Page.GoToAsync(TestConstants.ServerUrl + "/playground.html");19 await Page.SetContentAsync("<html><body><div class=\"second\"><div class=\"inner\">A</div></div></body></html>");20 var html = await Page.QuerySelectorAsync("html");21 var second = await html.XPathAsync("./body/div[contains(@class, 'second')]");22 var inner = await second[0].XPathAsync("./div[contains(@class, 'inner')]");23 var content = await Page.EvaluateFunctionAsync<string>("e => e.textContent", inner[0]);24 Assert.Equal("A", content);25 }26 [PuppeteerTest("queryselector.spec.ts", "ElementHandle.$x", "should return null for non-existing element")]27 [PuppeteerFact]28 public async Task ShouldReturnNullForNonExistingElement()29 {30 await Page.SetContentAsync("<html><body><div class=\"second\"><div class=\"inner\">B</div></div></body></html>");31 var html = await Page.QuerySelectorAsync("html");32 var second = await html.XPathAsync("/div[contains(@class, 'third')]");33 Assert.Empty(second);34 }35 }36}...
HandleExtensions.cs
Source:HandleExtensions.cs
...4namespace Wasari.Puppeteer5{6 public static class HandleExtensions7 {8 public static async Task<ElementHandle> SingleOrDefaultXPathAsync(this Page page,9 string xpath)10 {11 var handles = await page.XPathAsync(xpath);12 return handles.SingleOrDefault();13 }14 15 public static async Task<ElementHandle> SingleOrDefaultXPathAsync(this ElementHandle elementHandle,16 string xpath)17 {18 var handles = await elementHandle.XPathAsync(xpath);19 return handles.SingleOrDefault();20 }21 public static async Task<T> GetPropertyValue<T>(this JSHandle elementHandle, string property)22 {23 await using var propertyAsync = await elementHandle.GetPropertyAsync(property);24 if (propertyAsync != null)25 return await propertyAsync.JsonValueAsync<T>();26 return default;27 }28 }29}...
XPathAsync
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using PuppeteerSharp;7{8 {9 static void Main(string[] args)10 {11 MainAsync(args).GetAwaiter().GetResult();12 }13 static async Task MainAsync(string[] args)14 {15 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);16 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))17 using (var page = await browser.NewPageAsync())18 {19 await element.TypeAsync("PuppeteerSharp");20 }21 }22 }23}24public Task<ElementHandle> XPathAsync(string expression, object[] args = null);
XPathAsync
Using AI Code Generation
1var page = await browser.NewPageAsync();2var searchBox = await page.QuerySelectorAsync("input[title='Search']");3await searchBox.TypeAsync("PuppeteerSharp");4await searchBox.PressAsync("Enter");5await page.WaitForNavigationAsync();6foreach (var result in results)7{8 var text = await result.EvaluateFunctionAsync<string>("node => node.textContent");9 Console.WriteLine(text);10}11await browser.CloseAsync();12var page = await browser.NewPageAsync();13var searchBox = await page.QuerySelectorAsync("input[title='Search']");14await searchBox.TypeAsync("PuppeteerSharp");15await searchBox.PressAsync("Enter");16await page.WaitForNavigationAsync();17foreach (var result in results)18{19 var text = await result.EvaluateFunctionAsync<string>("node => node.textContent");20 Console.WriteLine(text);21}22await browser.CloseAsync();
XPathAsync
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using PuppeteerSharp;7using System.Diagnostics;8{9 {10 public static async Task<ElementHandle> XPathAsync(this ElementHandle elementHandle, string selector)11 {12 var document = await elementHandle.GetOwnerFrameAsync();13 return await document.XPathAsync(selector);14 }15 }16}17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22using PuppeteerSharp;23using System.Diagnostics;24{25 {26 public static async Task<ElementHandle> XPathAsync(this Frame frame, string selector)27 {28 var document = await frame.GetDocumentAsync();29 return await document.XPathAsync(selector);30 }31 }32}33using System;34using System.Collections.Generic;35using System.Linq;36using System.Text;37using System.Threading.Tasks;38using PuppeteerSharp;39using System.Diagnostics;40{41 {42 public static async Task<ElementHandle> XPathAsync(this Page page, string selector)43 {44 var document = await page.GetDocumentAsync();45 return await document.XPathAsync(selector);46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using PuppeteerSharp;55using System.Diagnostics;56{57 {58 public static async Task<ElementHandle> XPathAsync(this Browser browser, string selector)59 {60 var document = await browser.GetDocumentAsync();61 return await document.XPathAsync(selector);62 }63 }64}65using System;66using System.Collections.Generic;67using System.Linq;68using System.Text;69using System.Threading.Tasks;70using PuppeteerSharp;71using System.Diagnostics;72{73 {74 public static async Task<ElementHandle> XPathAsync(this ElementHandle
XPathAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 }))11 {12 using (var page = await browser.NewPageAsync())13 {14 var xpath = new XPathSelector(page);15 await title.TypeAsync("PuppeteerSharp");16 }17 }18 }19 }20}
XPathAsync
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 LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 var value = await elementHandle.EvaluateFunctionAsync<string>("(element) => element.getAttribute('value')");14 Console.WriteLine(value);15 await browser.CloseAsync();16 }17 }18}19using System;20using System.Threading.Tasks;21using PuppeteerSharp;22{23 {24 static async Task Main(string[] args)25 {26 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);27 var browser = await Puppeteer.LaunchAsync(new LaunchOptions28 {29 });30 var page = await browser.NewPageAsync();31 await page.TypeAsync("#search", "Hello World");32 var value = await elementHandle.EvaluateFunctionAsync<string>("(element) => element.getAttribute('value')");33 Console.WriteLine(value);34 await browser.CloseAsync();35 }36 }37}
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!!