Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NetworkHelper
- {
- /// <summary>
- /// Max attempts after a failed request.
- /// </summary>
- private static int _failedAttempts = 10;
- /// <summary>
- /// Get the html of a page waiting for a specific selector created by an ajax request.
- /// </summary>
- /// <param name="url">Link of the page.</param>
- /// <param name="selector">Selector to wait.</param>
- /// <returns>Html of the web page.</returns>
- public static async Task<string> LoadAndWaitForSelector(Uri url, string selector)
- {
- var browser = await Puppeteer.LaunchAsync(new LaunchOptions
- {
- Headless = true,
- ExecutablePath = Environment.GetEnvironmentVariable("CHROME_PATH"),
- });
- try
- {
- using (Page page = await browser.NewPageAsync())
- {
- await page.GoToAsync(url.ToString());
- await page.WaitForSelectorAsync(selector);
- return await page.GetContentAsync();
- }
- }
- catch (Exception ex)
- {
- //Scale the attempts and retry, server could be busy or the connection goes offline.
- if (_failedAttempts != 0)
- {
- _failedAttempts--;
- await Task.Delay(15000);
- }
- else
- {
- throw ex;
- }
- return await LoadAndWaitForSelector(url, selector);
- }
- }
- }
- var browser = await Puppeteer.LaunchAsync(new LaunchOptions
- {
- Headless = true,
- ExecutablePath = Environment.GetEnvironmentVariable("CHROME_PATH"),
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement