Best Puppeteer-sharp code snippet using PuppeteerSharp.Frame.GetContentAsync
PuppeteerBrowserBuilder.cs
Source:PuppeteerBrowserBuilder.cs
...258 async Task IPuppeteerBrowser.ClickAsync(Page page, string clickPlace, ClickOptions clickOptions)259 {260 await page.ClickAsync(clickPlace, clickOptions);261 } 262 async Task<string> IPuppeteerBrowser.GetContentAsync(Page page)263 {264 return await page.GetContentAsync();265 }266 async Task<Page> IPuppeteerBrowser.WaitGetPage(Page page, string selector, string targetId)267 { 268 var newTarget = await _browserContext.WaitForTargetAsync(a => a.Opener?.TargetId == targetId, new WaitForOptions { Timeout = 10 * 1000 });269 var newPage = await newTarget.PageAsync();270 await SetPageOptions(newPage);271 await newPage.WaitForSelectorAsync(selector);272 return newPage;273 }274 async Task IPuppeteerBrowser.CloseAsync()275 {276 await _browser.CloseAsync();277 }278 async Task<Page> IPuppeteerBrowser.ClickAndGetPage(ElementHandle element, string targetId)279 {280 await element.ClickAsync();281 var newTarget = await _browserContext.WaitForTargetAsync(a => a.Opener?.TargetId == targetId);282 var newPage = await newTarget.PageAsync();283 await SetPageOptions(newPage);284 return newPage;285 }286 async Task<Page> IPuppeteerBrowser.MouseClickAndGetPage(BoundingBox box, Page page)287 {288 int retryN = 0;289 return await _policyNoData.ExecuteAsync(async () =>290 {291 var pages = await _browserContext.PagesAsync();292 var pageCount = pages.Length;293 var listId = new List<string>();294 foreach (var page1 in pages)295 {296 var id = page1.Target?.TargetId;297 listId.Add(id);298 }299 await page.Keyboard.DownAsync("Control");300 await page.Mouse.ClickAsync(box.X + (box.Width / _diagonalClickDevisors[retryN]), box.Y + (box.Height / _diagonalClickDevisors[retryN]));301 await page.Keyboard.UpAsync("Control");302 var pages1 = await _browserContext.PagesAsync();303 if (pages1.Length <= pageCount)304 {305 retryN++;306 throw new TimeoutException();307 }308 var newPage = pages1.FirstOrDefault(a => !listId.Contains(a.Target?.TargetId));309 if (newPage == null)310 {311 throw new InvalidOperationException("Page not found after click");312 }313 if (_puppeteerContext.DialogHandler != null)314 {315 newPage.Dialog += _puppeteerContext.DialogHandler;316 }317 await SetPageOptions(newPage);318 return newPage;319 });320 }321 async Task<Page> IPuppeteerBrowser.ClickAndGetPage(ElementHandle element, string targetId, WaitForOptions waitForOptions)322 {323 await element.ClickAsync();324 var newTarget = await _browserContext.WaitForTargetAsync(a => a.Opener?.TargetId == targetId, waitForOptions);325 var newPage = await newTarget.PageAsync();326 await SetPageOptions(newPage);327 return newPage;328 }329 async Task<string> IPuppeteerBrowser.ClickButtonForContinue(Page page, string buttonXPath, string adUrl, params WaitUntilNavigation[] waitUntilNavigations)330 {331 var continueButtons = page.XPathAsync(buttonXPath);332 _logger.Info($"Needed click on button for continue. Url: {adUrl}");333 foreach (var continueButton in await continueButtons)334 {335 _logger.Debug($"Will click on {buttonXPath}");336 await continueButton.ClickAsync();337 var navigationTask = page.WaitForNavigationAsync(new NavigationOptions338 {339 WaitUntil = waitUntilNavigations340 });341 if (await Task.WhenAny(navigationTask, _delayService.DelayAsync(TimeSpan.FromSeconds(30))) == navigationTask)342 {343 _logger.Info($"Returned new url:'{page.Url}'");344 if (!page.Url.Contains("redirect/verify"))345 {346 return page.Url;347 }348 }349 return string.Empty;350 }351 return string.Empty;352 }353 async Task<Page[]> IPuppeteerBrowser.GetPages()354 {355 return await _browserContext.PagesAsync();356 }357 async Task<string> IPuppeteerBrowser.GetFramePage(string iframeSrc)358 {359 using (var multiVideopage = await _browserContext.NewPageAsync())360 {361 await SetPageOptions(multiVideopage);362 await multiVideopage.GoToAsync(iframeSrc, Convert.ToInt32(_puppeteerContext.PageLoadTimeout.TotalMilliseconds), new WaitUntilNavigation[] { WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load, WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2 });363 var data = await multiVideopage.GetContentAsync();364 return data;365 }366 }367 async Task<string> IPuppeteerBrowser.GetFramePage(Page page, string frameName)368 {369 var googlAdsFrame1 = page.Frames.ToList().FirstOrDefault(x => x.Name == frameName);370 if (googlAdsFrame1 == null)371 {372 return null;373 }374 return await googlAdsFrame1.GetContentAsync();375 }376 377 async Task<string> IPuppeteerBrowser.GetVideoAdsOnUrl(Page page, string xPath)378 {379 var elementToclick = await page.WaitForXPathAsync(xPath);380 if (elementToclick != null) 381 {382 await elementToclick.ClickAsync();383 return await page.EvaluateFunctionAsync<string>("() => navigator.clipboard.readText()");384 }385 _logger.Debug("GetVideoAdsOnUrl: elementToclick is null");386 return null;387 }388 /// <summary>389 /// Returns IpDetails by the specified url390 /// </summary>391 /// <param name="ipFetcherUrl">The ip details provider url.</param>392 /// <returns>The instance of <see cref="IpDetails"></see></returns>393 async Task<IpDetails> IPuppeteerBrowser.GetIpDetails(string ipFetcherUrl)394 {395 IpDetails ipDetails = await GetIpDetailsInternal(ipFetcherUrl);396 return ipDetails ?? new IpDetails397 {398 City = "N/A",399 ContinentName = "N/A",400 CountryCode = "N/A",401 CountryName = "N/A",402 IpAddress = "127.0.0.1",403 State = "N/A",404 ContinentCode = "N/A"405 };406 }407 /// <summary>408 /// Returns IpDetails by the specified url and, in case of error, returns the default country code and country name409 /// </summary>410 /// <param name="ipFetcherUrl">The ip details provider url.</param>411 /// <param name="defaultCountryCode">The country code tu return default details.</param>412 /// <returns>The instance of <see cref="IpDetails"></see></returns>413 async Task<IpDetails> IPuppeteerBrowser.GetIpDetails(string ipFetcherUrl, string defaultCountryCode)414 {415 IpDetails ipDetails = await GetIpDetailsInternal(ipFetcherUrl);416 return ipDetails ?? new IpDetails417 {418 City = "N/A",419 ContinentName = "N/A",420 CountryCode = defaultCountryCode,421 CountryName = References.Countries[defaultCountryCode],422 IpAddress = "127.0.0.1",423 State = "N/A",424 ContinentCode = "N/A"425 };426 }427 async Task<string> IPuppeteerBrowser.GetPageContent(string stringUrl, params WaitUntilNavigation[] waitUntilNavigations)428 {429 return await GetPageContentInternalAsync(stringUrl, waitUntilNavigations);430 }431 private async Task SetPageOptions(Page page)432 {433 var viewPortOptions = new ViewPortOptions434 { Width = _browserConfig.WindowWidth, Height = _browserConfig.WindowHeight };435 await page.SetViewportAsync(viewPortOptions);436 if (!string.IsNullOrWhiteSpace(_puppeteerContext.ProxyUserName))437 {438 await page.AuthenticateAsync(new Credentials439 {440 Username = _puppeteerContext.ProxyUserName,441 Password = _puppeteerContext.ProxyPwd442 });443 }444 }445 async Task<Page> IPuppeteerBrowser.GetPage(string url, TimeSpan? timeoutPageLoad, params WaitUntilNavigation[] waitUntilNavigations)446 {447 var page = await GoToPageAsync(url, timeoutPageLoad, waitUntilNavigations);448 if (_overridePermissions?.Any() ?? false)449 {450 await page.BrowserContext.OverridePermissionsAsync(page.Url,451 new []452 {453 OverridePermission.ClipboardWrite, OverridePermission.ClipboardRead,454 OverridePermission.AccessibilityEvents, OverridePermission.BackgroundSync,455 OverridePermission.Geolocation, OverridePermission.Microphone, OverridePermission.Notifications,456 OverridePermission.PaymentHandler/*, OverridePermission.Push*/457 });458 }459 return page;460 }461 async Task<string> IPuppeteerBrowser.GetScrolledPage(string url, int maxScroll, params WaitUntilNavigation[] waitUntilNavigations)462 {463 var pageResult = string.Empty;464 try465 {466 using (var page = await GoToPageAsync(url, null, waitUntilNavigations))467 {468 await ScrollPageSync(page, TimeSpan.FromMilliseconds(100));469 pageResult = await page.GetContentAsync();470 }471 }472 catch (Exception ex)473 {474 _logger.Error(ex);475 }476 return pageResult;477 }478 async Task IPuppeteerBrowser.ScrollPageSync(Page page, int maxScroll = 40000, int step = 200)479 {480 await ScrollPageSync(page, TimeSpan.FromMilliseconds(100), maxScroll, step);481 }482 /// <summary>483 /// Return state of page permission. Exmp: permissionName=clipboard-read484 /// </summary>485 /// <returns></returns>486 async Task<string> IPuppeteerBrowser.GetPagePermission(Page page, string permissionName)487 {488 return await page.EvaluateFunctionAsync<string>($"name => navigator.permissions.query({permissionName}).then(result => result.state)");489 }490 public async Task ScrollPageSync(Page page, TimeSpan stepDelay, int maxScroll = 40000, int step = 200)491 {492 var height = await page.EvaluateExpressionAsync<int>(@"document.body.scrollHeight");493 var x = 0;494 while (x < height && x <= maxScroll)495 {496 height = await page.EvaluateExpressionAsync<int>(@"document.body.scrollHeight");497 x += 200;498 await _delayService.DelayAsync(stepDelay);499 await page.EvaluateExpressionAsync($"window.scrollTo(0, {x});");500 }501 }502 private async Task<Page> GoToPageAsync(string stringUrl, TimeSpan? timeSpan, params WaitUntilNavigation[] waitUntilNavigations)503 {504 var page = await _browserContext.NewPageAsync();505 await page.SetCacheEnabledAsync(_puppeteerContext.PageCacheEnabled);506 await page.SetJavaScriptEnabledAsync(true);507 if (_puppeteerContext.PageEvents)508 {509 page.Error += Page_Error;510 page.PageError += Page_PageError;511 page.Console += Page_Info;512 page.Request += Page_Request;513 }514 if (_puppeteerContext.DialogHandler != null)515 {516 page.Dialog += _puppeteerContext.DialogHandler;517 }518 await SetPageOptions(page);519 var resp = await page.GoToAsync(stringUrl,520 Convert.ToInt32((timeSpan ?? TimeSpan.FromSeconds(30)).Milliseconds), waitUntilNavigations);521 LogGoToResponse(stringUrl, resp);522 return page;523 }524 private void Page_PageError(object sender, PageErrorEventArgs e)525 {526 _logger.Error(e.Message);527 }528 private void Page_Request(object sender, RequestEventArgs e)529 {530 _logger.Debug($"{e.Request?.Method}:{e.Request?.ResourceType.ToString()}:{e.Request?.Url}");531 }532 private void LogGoToResponse(string stringUrl, Response resp)533 {534 if (resp != null)535 {536 var respLogEvent = new LogEventInfo(LogLevel.Info, string.Empty, $"goto response to {stringUrl}");537 respLogEvent.Properties["FromCache"] = resp.FromCache;538 respLogEvent.Properties["Url"] = resp.Url;539 respLogEvent.Properties["RemoteAddress.IP"] = resp.RemoteAddress?.IP;540 respLogEvent.Properties["RemoteAddress.Port"] = resp.RemoteAddress?.Port;541 respLogEvent.Properties["Request.RequestId"] = resp.Request?.RequestId;542 respLogEvent.Properties["Status"] = resp.Status;543 respLogEvent.Properties["Ok"] = resp.Ok;544 respLogEvent.Properties["FromServiceWorker"] = resp.FromServiceWorker;545 if (resp.Headers != null)546 {547 foreach (var header in resp.Headers)548 {549 respLogEvent.Properties[$"Header:{header.Key}"] = header.Value;550 }551 }552 _logger.Log(respLogEvent);553 }554 }555 private void Page_Info(object sender, ConsoleEventArgs e)556 {557 _logger.Info($"{e.Message.Type}: {e.Message.Text}");558 }559 private void Page_Error(object sender, PuppeteerSharp.ErrorEventArgs e)560 {561 _logger.Error(e.Error);562 }563 public void Dispose()564 {565 _logger.Info($"Disposing browser {_browser.Process.Id}");566 _browser?.Dispose();567 }568 private string StripHtmlTags(string input) => RegexStripHtmlTags.Replace(input, string.Empty); 569 private async Task<string> GetPageContentInternalAsync(string stringUrl, params WaitUntilNavigation[] waitUntilNavigations)570 {571 using (var page = await _browserContext.NewPageAsync())572 {573 await SetPageOptions(page);574 var resp = await page.GoToAsync(stringUrl, null, waitUntilNavigations);575 LogGoToResponse(stringUrl, resp);576 return await page.GetContentAsync();577 }578 }579 private async Task<IpDetails> GetIpDetailsInternal(string ipFetcherUrl)580 {581 IpDetails ipDetails = null;582 try583 {584 var ipPageSource = await GetPageContentInternalAsync(ipFetcherUrl, WaitUntilNavigation.Networkidle0);585 _logger.Info(ipPageSource);586 var ipPage = StripHtmlTags(ipPageSource);587 if (!ipPage.Contains("OVER_QUERY_LIMIT"))588 {589 ipDetails = JsonConvert.DeserializeObject<IpDetails>(ipPage);590 return new IpDetails()...
Program.cs
Source:Program.cs
...133 break;134 }135 }136 Console.WriteLine($"ééç»å½ç½å:{LoginFrame.Url}");137 var Content = await LoginFrame.GetContentAsync();138 var login = await LoginFrame.XPathAsync(@"/html/body/div/div/div[1]/div/div[1]/div[1]/img");139 var QrCodeSrc = await login[0].GetPropertyAsync("src");140 //Console.WriteLine(QrCodeSrc);141 var guid = Guid.NewGuid();142 await pushPlus.SendMsg(@$"äºç»´ç 50ç§åè¿æ</br>æ¨çGuid为:{guid}</br>请ä¿åäºç»´ç åå°å¦ä¹ 强å½æ«ç </br><img src='{QrCodeSrc.ToString().Replace("JSHandle:", "")}'>", "ç§æ强å½ç»å½");143 Console.WriteLine($"Guid:{guid} å·²åéç»å½æ¨é...");144 Console.WriteLine($"çå¾
ç»å½,50ç§åèªå¨éåº");145 int i = 0;146 while (true)147 {148 var cookies = await page.GetCookiesAsync();149 if (cookies != null)150 {151 foreach (var item in cookies)152 {153 if (item.Name == "token")154 {155 Program.cookies = cookies;156 //File.AppendAllText(Json)157 //Console.WriteLine($"Guid:{guid}å·²ç»å½");158 return false;159 }160 }161 }162 Thread.Sleep(1000 * 1);163 i++;164 Console.WriteLine($"å·²ç»è¿å»{i}ç§");165 if (i >= 50)166 {167 await pushPlus.SendMsg(@$"GUID:{guid}</br><ç»å½è¶
æ¶", "ç§æ强å½ç»å½è¶
æ¶");168 Console.WriteLine($"Guid:{guid} ç»å½è¶
æ¶ èªå¨éåº");169 break;170 }171 }172 Thread.Sleep(1000 * 30);173 };174 };175 Console.WriteLine("----------------------------------------------------------------------");176 return true;177 }178 /// <summary>179 /// 模æè°·ææç´¢180 /// </summary>181 /// <param name="text"></param>182 /// <returns></returns>183 public static async Task<bool> GoogleSeach(string text)184 {185 await using (browser)186 {187 await using (var page = await browser.NewPageAsync())188 {189 await page.GoToAsync("https://www.google.com/");190 //AnsiConsole.WriteLine("å¼å§è·åè¾å
¥æ¡ç¦ç¹");191 //await page.ClickAsync("input[id='kw']");192 AnsiConsole.WriteLine("çå¾
ä¸ç§");193 await page.WaitForTimeoutAsync(1000);194 AnsiConsole.WriteLine("模æè¾å
¥ææ¬");195 await page.TypeAsync("input[class='gLFyf gsfi']", text);196 AnsiConsole.WriteLine("çå¾
ä¸ç§");197 await page.WaitForTimeoutAsync(1000);198 AnsiConsole.WriteLine("模ææä¸Enteré®ç");199 await page.Keyboard.PressAsync("Enter");200 await page.WaitForNavigationAsync(); //注æ å¦æ导èªåçäºåå å¿
须使ç¨çå¾
导èª201 var Content = await page.GetContentAsync(); //è·å页é¢å
容202 var yuRUbf = await page.QuerySelectorAllAsync("div[class='yuRUbf']"); //è·åclass为yuRUbfæædiv203 AnsiConsole.WriteLine($"è·åå°å:{yuRUbf.Length}æ¡æç´¢æ°æ®\n");204 foreach (var item in yuRUbf)205 {206 var Tittle = await item.QuerySelectorAllAsync(@"a>h3");207 var Url = await item.QuerySelectorAllAsync(@"a");208 var StrTittle = await Tittle[0].GetPropertyAsync("innerText");209 var StrUrl = await Url[0].GetPropertyAsync("href");210 AnsiConsole.WriteLine($"æ é¢:{StrTittle.ToString().Replace("JSHandle:", "")}\nå°å:{StrUrl.ToString().Replace("JSHandle:", "")}\n\n");211 }212 AnsiConsole.WriteLine("çå¾
ä¸ç§");213 await page.WaitForTimeoutAsync(3000);214 AnsiConsole.WriteLine("ç»æ");215 };216 };217 return true;218 }219 /// <summary>220 /// å½å¼å¤è¯¾ç¨ç®åçè§é¢çæç« 221 /// </summary>222 /// <returns></returns>223 public static async void Ouchn()224 {225 var CourseList = new Dictionary<string, Page>();226 var Task = new List<Task>();227 var Pages = await browser.PagesAsync(); //è·åå½åææpage228 Console.WriteLine($"请æç
§æç¨æ¥,è¦å
³éææçEdgeçé¢");229 Console.WriteLine($"课ç¨ç»æ请è¾å
¥exitéåºè¯¾ç¨,å¨è¯¾ç¨æªå®ææ¶ä¹å¯ä»¥è¾å
¥exitéåº");230 Console.WriteLine($"课ç¨ç»æ请è¾å
¥exitéåºè¯¾ç¨,å¨è¯¾ç¨æªå®ææ¶ä¹å¯ä»¥è¾å
¥exitéåº");231 Console.WriteLine($"课ç¨ç»æ请è¾å
¥exitéåºè¯¾ç¨,å¨è¯¾ç¨æªå®ææ¶ä¹å¯ä»¥è¾å
¥exitéåº");232 Thread.Sleep(1000 * 5);233 Console.WriteLine($"è·åå°å½åæµè§å¨é¡µé¢æ:{Pages.Length}个");234 if (Pages.Length > 0)235 {236 Console.WriteLine("å¼å§å¹é
ææé¾æ¥æ¯å¦ä¸ºå½å¼è¯¾ç¨é¡µé¢...");237 for (int i = 0; i < Pages.Length; i++)238 {239 var Page = Pages[i];240 var Url = Page.Url.ToString();241 var Name = await Page.GetTitleAsync();242 if (Regex.IsMatch(Url, @"http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?id=[0-9]*"))243 {244 Console.WriteLine($"å¹é
æå:({Name}){Url}");245 CourseList.Add(Name, Page);246 }247 }248 if (CourseList.Count > 0)249 {250 Console.WriteLine($"å¹é
å°ç¬¦åå½å¼è¯¾ç¨é¡µé¢æ»æ°:{CourseList.Count}个");251 }252 else253 {254 Console.WriteLine($"没æå¹é
å°ç¬¦åå½å¼è¯¾ç¨é¡µé¢,请è¾å
¥ä»»æé®éåº...");255 Console.ReadLine();256 }257 }258 else259 {260 Console.WriteLine($"å½åè·åå°é¡µé¢ä¸º0,请è¾å
¥ä»»æé®éåº...");261 Console.ReadLine();262 }263 Console.WriteLine($"\nå¼å§å建线ç¨...");264 foreach (var item in CourseList)265 {266 var Tas = new System.Threading.Tasks.Task(async () =>267 {268 var Links = await item.Value.QuerySelectorAllAsync("a[class='aalink']"); //è·å第ä¸ä¸ªé¡µé¢çææaæ ç¾class为aalinkç269 Console.WriteLine($"[课ç¨:{item.Key}]è·åå°{Links.Length}个é¾æ¥");270 async Task<bool> OpenLink(ElementHandle[] handles)271 {272 try273 {274 var r = new Random();275 for (int i = 0; i < Links.Length; i++)276 {277 var URL = await Links[i].GetPropertyAsync("href");278 if (URL.ToString().Replace("JSHandle:", "").Contains(""))279 {280 }281 var Page = await browser.NewPageAsync();282 await Page.GoToAsync(URL.ToString().Replace("JSHandle:", ""), new NavigationOptions() { });283 Console.WriteLine($"[{item.Key}]å½åæ£å¨æµè§ç¬¬{i}个é¾æ¥:{URL.ToString().Replace("JSHandle:", "")}");284 var s = r.Next(5, 10);285 Console.WriteLine($"[{item.Key}]çå¾
{s}ç§");286 await Page.WaitForTimeoutAsync(s * 1000);287 await Page.CloseAsync(new PageCloseOptions() { RunBeforeUnload = true });288 Console.WriteLine($"[{item.Key}]å
³é页é¢");289 Console.WriteLine($"å¼å§ä¸ä¸ä¸ª\n");290 }291 }292 catch293 {294 }295 return true;296 }297 await OpenLink(Links);298 Console.WriteLine($"[{item.Key}]å¦ä¹ ç»æ å·²å¦ä¹ {Links.Length}个é¾æ¥\n");299 });300 Task.Add(Tas);301 }302 Console.WriteLine($"å建æåï¼æ»æ°{Task.Count}个\n");303 Console.WriteLine($"å¼å§æ§è¡ï¼\n");304 Task.ForEach(Course => { Course.Start(); });305 Thread.Sleep(1000 * 5);306 void Exit()307 {308 var msg = Console.ReadLine();309 if (msg == "exit")310 {311 }312 else313 {314 Console.WriteLine($"请è¾å
¥exitéåºå¦ä¹ ...");315 Exit();316 }317 }318 Exit();319 }320 #endregion321 #region åºæ¬ä½¿ç¨æ¹æ³322 /// <summary>323 /// æ£æ¥ä¸è½½æµè§å¨324 /// </summary>325 /// <returns></returns>326 public async static Task<bool> CheckDownloadBrowser()327 {328 Console.WriteLine("----------------------------------------------------------------------");329 var br = new BrowserFetcher();330 var ExecutablePath = await br.GetRevisionInfoAsync();331 var Downloaded = ExecutablePath.Downloaded;332 Console.WriteLine($"æ£æ¥å½åæ¯å¦ä¸è½½æµè§å¨:{(Downloaded == true ? "å·²ä¸è½½" : "æªä¸è½½")}");333 if (!Downloaded)334 {335 Console.WriteLine($"åå¤å¼å§ä¸è½½æµè§å¨");336 Console.WriteLine($"å¹³å°:{ExecutablePath.Platform}");337 Console.WriteLine($"å½åé»è®¤éæ©æµè§å¨:{br.Product}");338 Console.WriteLine($"é»è®¤ä¸è½½æµè§å¨çæ¬:{ExecutablePath.Revision}");339 /*Console.WriteLine($"Local:{ExecutablePath.Local}");340 Console.WriteLine($"æ件夹路å¾:{ExecutablePath.FolderPath}");341 Console.WriteLine($"æ§è¡æ件路å¾:{ExecutablePath.ExecutablePath}");342 Console.WriteLine($"æå¨ä¸è½½å°å:{ExecutablePath.Url}");*/343 Console.WriteLine("--------------------");344 AnsiConsole.MarkupLine("[green]å¼å§[/]ä¸è½½æµè§å¨");345 br.DownloadProgressChanged += (a, b) => //ä¸è½½è¿åº¦æ¡äºä»¶346 {347 if (b.ProgressPercentage >= 100)348 {349 AnsiConsole.MarkupLine("ä¸è½½æµè§å¨[red]å®æ¯[/]");350 }351 };352 await br.DownloadAsync();353 }354 else355 {356 Console.WriteLine($"å¹³å°:{ExecutablePath.Platform}");357 Console.WriteLine($"å½åæµè§å¨:{br.Product}");358 Console.WriteLine($"å½åæµè§å¨çæ¬:{ExecutablePath.Revision}");359 }360 Console.WriteLine("----------------------------------------------------------------------");361 return true;362 }363 /// <summary>364 /// ç½é¡µæªå¾365 /// </summary>366 /// <param name="url">ç½å</param>367 /// <param name="outputFile">è¾åºå°å</param>368 /// <returns></returns>369 public async static Task<bool> TakeScreenshots(string url, string outputFile)370 {371 await using (browser)372 {373 await using (var page = await browser.NewPageAsync())374 {375 await page.GoToAsync(url);376 await page.ScreenshotAsync(outputFile);377 //await page.ScreenshotBase64Async(outputFile);378 //await page.ScreenshotDataAsync(outputFile);379 //await page.ScreenshotStreamAsync(outputFile);380 };381 };382 return true;383 }384 /// <summary>385 /// ç½é¡µpdfçæ386 /// </summary>387 /// <param name="url">ç½å</param>388 /// <param name="outputFile">è¾åºå°å</param>389 /// <returns></returns>390 public async static Task<bool> GeneratePdf(string url, string outputFile)391 {392 await using (browser)393 {394 await using (browser)395 {396 await using (var page = await browser.NewPageAsync())397 {398 await page.GoToAsync(url);399 await page.PdfAsync(outputFile);400 /*await page.PdfAsync(outputFile, new PdfOptions //diy pfd设置401 {402 Format = PaperFormat.A4,403 DisplayHeaderFooter = true,404 MarginOptions = new MarginOptions405 {406 Top = "20px",407 Right = "20px",408 Bottom = "40px",409 Left = "20px"410 },411 FooterTemplate = "<div id=\"footer-template\" style=\"font-size:10px !important; color:#808080; padding-left:10px\">Footer Text</div>"412 });*/413 //await page.PdfDataAsync(outputFile);414 //await page.PdfStreamAsync(outputFile);415 };416 };417 }418 return true;419 }420 /// <summary>421 /// 注å
¥html422 /// </summary>423 /// <returns></returns>424 public async static Task<bool> InjectHTML(string url, string html)425 {426 await using (browser)427 {428 using (var page = await browser.NewPageAsync())429 {430 await page.GoToAsync(url);431 //await page.SetContentAsync("<div>My Receipt</div>");432 await page.SetContentAsync(html);433 var result = await page.GetContentAsync(); //è·åç½é¡µhtml434 }435 }436 return true;437 }438 /// <summary>439 /// EvaluateJavascript440 /// </summary>441 /// <returns></returns>442 public async static Task<bool> EvaluateJavascript(string url)443 {444 await using (browser)445 {446 using (var page = await browser.NewPageAsync())447 {...
PuppeteerMethods.cs
Source:PuppeteerMethods.cs
...91 using (var page = await OpenChromiumPage())92 {93 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };94 await page.GoToAsync("http://www.google.com", new NavigationOptions { WaitUntil = waitUntil });95 return await page.GetContentAsync();96 }97 }98 public static async Task<string> SetHtmlToPage()99 {100 using (var page = await OpenChromiumPage())101 {102 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };103 await page.SetContentAsync("<div><h1>Hello World!</h1></div>");104 return await page.GetContentAsync();105 }106 }107 public static async Task<PuppeteerDataResult<string>> GetTitleOfPage(string url)108 {109 using (var page = await OpenChromiumPage())110 {111 try112 {113 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };114 await page.GoToAsync(url, new NavigationOptions { WaitUntil = waitUntil });115 var title = await page.GetTitleAsync();116 return new SuccessPuppeteerDataResult<string>(title, "Get Title Successfully");117 }118 catch (Exception ex)119 {120 return new ErrorPuppeteerDataResult<string>("Error Occured! Detail: " + ex.Message);121 }122 finally123 {124 CloseChromium(page);125 }126 }127 }128 public static async Task<Frame[]> GetAllFrames()129 {130 using (var page = await OpenChromiumPage())131 {132 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };133 await page.GoToAsync("https://www.google.com", new NavigationOptions { WaitUntil = waitUntil });134 return page.Frames;135 }136 }137 public static async Task<PuppeteerResult> LoginFacebook(string username, string password)138 {139 using (var page = await OpenChromiumPage())140 {141 try142 {143 WaitUntilNavigation[] waitUntil = new[] { WaitUntilNavigation.Networkidle0, WaitUntilNavigation.Networkidle2, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Load };144 await page.GoToAsync("https://tr-tr.facebook.com/", new NavigationOptions { WaitUntil = waitUntil });145 await page.WaitForSelectorAsync("input#email");146 //You must change your facebook login informations.147 await page.TypeAsync("input#email", username);148 await page.TypeAsync("input#pass", password);149 await page.ClickAsync("button[name='login']");150 await page.WaitForNavigationAsync();151 var HtmlContent = await page.GetContentAsync();152 var Cookie = await page.GetCookiesAsync();153 var TitleOfPage = await page.GetTitleAsync();154 var filePath = "D:\\PdfFiles";155 var fileName = Guid.NewGuid() + ".pdf";156 if (!Directory.Exists(filePath))157 Directory.CreateDirectory(filePath);158 await page.PdfAsync(Path.Combine(filePath, fileName));159 return new SuccessPuppeteerResult("Pdf Created Succesfully");160 }161 catch (Exception ex)162 {163 return new ErrorPuppeteerResult("Error Occured! Detail: " + ex.Message);164 }165 finally...
FetchDynamicResource.xaml.cs
Source:FetchDynamicResource.xaml.cs
...354 var page = await browser.NewPageAsync();355 ShowStatusTextPuppeteer($"æå¼ç½å{url}");356 await page.GoToAsync(url);357358 var html = await page.GetContentAsync();359360 //å
³éæµè§å¨361 browser.Disconnect();362 browser.Dispose();363 ShowStatusTextPuppeteer("就绪");364 return html;365 }366367 private void ShowStatusTextPuppeteer(string text)368 {369 this.Dispatcher.Invoke(() => {370 this.lbl_StatusText_Puppeteer.Content = text;371 });372 }
...
汽车之家.cs
Source:汽车之家.cs
...24 // var frame1 = page.Frames.First(f => f.Name == "frame1");25 // var frame2 = page.MainFrame;26 // var frame1 = frame2.ChildFrames.First();27 // var waitForXPathPromise = frame1.WaitForXPathAsync("//div");28 //var test=await frame1.GetContentAsync();29 // var ifrmElement = await page.WaitForSelectorAsync("#reply-list");30 // ifrmElement.31 //var ifrmFrame = await ifrmElement.ContentFrameAsync();32 //var ifrmHtml = await ifrmFrame.GetContentAsync();33 // var test = await page.SelectAsync("#J-datetime-select > a:nth-child(3)");34 var result = new List<CarFamilyDatas>();35 int n = 0;36 while (n++ < 29)37 {38 CreateModelWithAngleSharp(await page.GetContentAsync(), result);39 await page.ClickAsync("#topPager > div > a.page-item-next");40 }41 CarFamilyDatas.IntrusiveExport(result);42 //var replaylist = await page.SelectAsync("#reply-list");43 44 //// await ifrmElement.ClickAsync();45 //var ifrmElement1 = await page.XPathAsync("//*[@id=\"reply-item-72217813\"]");46 //await ifrmElement1[0].ClickAsync();47 //await page.ClickAsync("body > div:nth-child(24) > ul > li:nth-child(5)");48 //var beginDate = await page.SelectAsync("#beginDate");49 //await page.ClickAsync("#beginDate");50 //await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");51 //await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");52 //await page.SelectAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");53 //await page.ClickAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");54 //await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");55 //await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");56 //await page.SelectAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");57 //await page.ClickAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");58 //await page.SelectAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");59 //await page.ClickAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");60 ////Get and return the HTML content of the page61 //var htmlString = await page.GetContentAsync();62 }63 private static void CreateModelWithAngleSharp(string html, List<CarFamilyDatas> result)64 {65 var config = Configuration.Default;66 var context = BrowsingContext.New(config);67 var parser = context.GetService<IHtmlParser>();68 var source = html;69 var node = parser.ParseDocument(source);70 var list=node.QuerySelector("#reply-list");71 var content =list.TextContent.Replace("举æ¥", "").Replace("æ¥ç对è¯","").Replace(" +1顶","");72 var lines= content.Split("åå¤");73 foreach (var line in lines)74 {75 int count;...
RunChoutiCraper.cs
Source:RunChoutiCraper.cs
...24 // var frame1 = page.Frames.First(f => f.Name == "frame1");25 // var frame2 = page.MainFrame;26 // var frame1 = frame2.ChildFrames.First();27 // var waitForXPathPromise = frame1.WaitForXPathAsync("//div");28 //var test=await frame1.GetContentAsync();29 // var ifrmElement = await page.WaitForSelectorAsync("#reply-list");30 // ifrmElement.31 //var ifrmFrame = await ifrmElement.ContentFrameAsync();32 //var ifrmHtml = await ifrmFrame.GetContentAsync();33 // var test = await page.SelectAsync("#J-datetime-select > a:nth-child(3)");34 var result = new List<CarFamilyDatas>();35 int n = 0;36 while (n++ < 29)37 {38 CreateModelWithAngleSharp(await page.GetContentAsync(), result);39 await page.ClickAsync("#topPager > div > a.page-item-next");40 }41 CarFamilyDatas.IntrusiveExport(result);42 //var replaylist = await page.SelectAsync("#reply-list");43 //// await ifrmElement.ClickAsync();44 //var ifrmElement1 = await page.XPathAsync("//*[@id=\"reply-item-72217813\"]");45 //await ifrmElement1[0].ClickAsync();46 //await page.ClickAsync("body > div:nth-child(24) > ul > li:nth-child(5)");47 //var beginDate = await page.SelectAsync("#beginDate");48 //await page.ClickAsync("#beginDate");49 //await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");50 //await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");51 //await page.SelectAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");52 //await page.ClickAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");53 //await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");54 //await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");55 //await page.SelectAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");56 //await page.ClickAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");57 //await page.SelectAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");58 //await page.ClickAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");59 ////Get and return the HTML content of the page60 //var htmlString = await page.GetContentAsync();61 }62 private static void CreateModelWithAngleSharp(string html, List<CarFamilyDatas> result)63 {64 var config = Configuration.Default;65 var context = BrowsingContext.New(config);66 var parser = context.GetService<IHtmlParser>();67 var source = html;68 var node = parser.ParseDocument(source);69 var list = node.QuerySelector("#reply-list");70 var content = list.TextContent.Replace("举æ¥", "").Replace("æ¥ç对è¯", "").Replace(" +1顶", "");71 var lines = content.Split("åå¤");72 foreach (var line in lines)73 {74 int count;...
zhifubao.cs
Source:zhifubao.cs
...20 // var frame1 = page.Frames.First(f => f.Name == "frame1"); 21 // var frame2 = page.MainFrame;22 // var frame1 = frame2.ChildFrames.First(); 23 // var waitForXPathPromise = frame1.WaitForXPathAsync("//div");24 //var test=await frame1.GetContentAsync();25 var ifrmElement = await page.WaitForSelectorAsync("#J-datetime-select > a:nth-child(3)");26 // ifrmElement.27 //var ifrmFrame = await ifrmElement.ContentFrameAsync();28 //var ifrmHtml = await ifrmFrame.GetContentAsync();29 // var test = await page.SelectAsync("#J-datetime-select > a:nth-child(3)");30 await page.ClickAsync("#J-datetime-select > a:nth-child(3)");31 await page.ClickAsync("body > div:nth-child(19) > ul > li:nth-child(5)");32 // await ifrmElement.ClickAsync();33 var ifrmElement1 = await page.XPathAsync("/html/body/div[10]/ul/li[5]");34 await ifrmElement1[0].ClickAsync();35 await page.ClickAsync("body > div:nth-child(24) > ul > li:nth-child(5)");36 var beginDate = await page.SelectAsync("#beginDate");37 await page.ClickAsync("#beginDate");38 await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");39 await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #beginDate");40 await page.SelectAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");41 await page.ClickAsync(".ui-calendar:nth-child(39) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column > .focused-element");42 await page.SelectAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");43 await page.ClickAsync(".record-search-option-date > #J-search-date-container > #J-datetime-select #endDate");44 await page.SelectAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");45 await page.ClickAsync(".ui-calendar:nth-child(40) > .ui-calendar > .ui-calendar-data-container > .ui-calendar-date-column:nth-child(5) > .ui-calendar-day-0");46 await page.SelectAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");47 await page.ClickAsync("#main > div.amount-top > div > div.fn-clear.action-other.action-other-show > div.fn-left > div > a.J-download-tip.mr-10");48 //Get and return the HTML content of the page49 var htmlString = await page.GetContentAsync();50 }51 }52}...
IPuppeteerBrowser.cs
Source:IPuppeteerBrowser.cs
...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);...
GetContentAsync
Using AI Code Generation
1var browserFetcher = new BrowserFetcher();2await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);3var browser = await Puppeteer.LaunchAsync(new LaunchOptions4{5});6var page = await browser.NewPageAsync();7var content = await page.GetContentAsync();8Console.WriteLine(content);9Console.ReadLine();10await browser.CloseAsync();
GetContentAsync
Using AI Code Generation
1var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"4});5var page = await browser.NewPageAsync();6var content = await page.GetContentAsync();7Console.WriteLine(content);8await browser.CloseAsync();
GetContentAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().Wait();9 }10 static async Task MainAsync()11 {12 var options = new LaunchOptions { Headless = true };13 using (var browser = await Puppeteer.LaunchAsync(options))14 using (var page = await browser.NewPageAsync())15 {16 await page.WaitForSelectorAsync("input[name=q]");17 await page.TypeAsync("input[name=q]", "PuppeteerSharp");18 await page.ClickAsync("input[type=submit]");19 await page.WaitForNavigationAsync();20 string content = await page.GetContentAsync();21 Console.WriteLine(content);22 }23 }24 }25}26public Task<string> GetContentAsync()
GetContentAsync
Using AI Code Generation
1var page = await browser.NewPageAsync();2var element = await page.QuerySelectorAsync("input[name=q]");3await element.TypeAsync("PuppeteerSharp");4await page.Keyboard.PressAsync("Enter");5await page.WaitForNavigationAsync();6var content = await page.GetContentAsync();7var page = await browser.NewPageAsync();8var element = await page.QuerySelectorAsync("input[name=q]");9await element.TypeAsync("PuppeteerSharp");10await page.Keyboard.PressAsync("Enter");11await page.WaitForNavigationAsync();12var content = await page.GetContentAsync();13var page = await browser.NewPageAsync();14var element = await page.QuerySelectorAsync("input[name=q]");15await element.TypeAsync("PuppeteerSharp");16await page.Keyboard.PressAsync("Enter");17await page.WaitForNavigationAsync();18var content = await page.GetContentAsync();19var page = await browser.NewPageAsync();20var element = await page.QuerySelectorAsync("input[name=q]");21await element.TypeAsync("PuppeteerSharp");22await page.Keyboard.PressAsync("Enter");23await page.WaitForNavigationAsync();24var content = await page.GetContentAsync();25var page = await browser.NewPageAsync();26var element = await page.QuerySelectorAsync("input[name=q]");27await element.TypeAsync("PuppeteerSharp");28await page.Keyboard.PressAsync("Enter");29await page.WaitForNavigationAsync();30var content = await page.GetContentAsync();31var page = await browser.NewPageAsync();32var element = await page.QuerySelectorAsync("input[name=q]");33await element.TypeAsync("Puppeteer
GetContentAsync
Using AI Code Generation
1var browser = await Puppeteer.LaunchAsync(new LaunchOptions2{3 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"4});5var page = await browser.NewPageAsync();6var frame = page.MainFrame;7var content = await frame.GetContentAsync();8Console.WriteLine(content);9await browser.CloseAsync();10var browser = await Puppeteer.LaunchAsync(new LaunchOptions11{12 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"13});14var page = await browser.NewPageAsync();15var frame = page.MainFrame;16var content = await frame.GetContentAsync();17Console.WriteLine(content);18await browser.CloseAsync();19var browser = await Puppeteer.LaunchAsync(new LaunchOptions20{21 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"22});23var page = await browser.NewPageAsync();24var frame = page.MainFrame;25var content = await frame.GetContentAsync();26Console.WriteLine(content);27await browser.CloseAsync();28var browser = await Puppeteer.LaunchAsync(new LaunchOptions29{30 ExecutablePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"31});32var page = await browser.NewPageAsync();33var frame = page.MainFrame;34var content = await frame.GetContentAsync();35Console.WriteLine(content);36await browser.CloseAsync();
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!!