Created
March 31, 2020 19:38
-
-
Save sorashi/98bd96b7cba69ece69c153fb47fd4c93 to your computer and use it in GitHub Desktop.
Get page source after javascript execution using CefSharp.Offscreen (chromium)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Task LoadPageAsync(IWebBrowser browser) { | |
var tcs = new TaskCompletionSource<bool>(); | |
void Handler(object sender, LoadingStateChangedEventArgs args) { | |
if (args.IsLoading) return; | |
browser.LoadingStateChanged -= Handler; | |
tcs.TrySetResult(true); | |
} | |
browser.LoadingStateChanged += Handler; | |
return tcs.Task; | |
} | |
private static async Task<string> GetPageSourceAsync(string url) { | |
using (var browser = new ChromiumWebBrowser(url, new BrowserSettings { | |
WindowlessFrameRate = 1 })) { | |
await LoadPageAsync(browser); | |
return await browser.GetSourceAsync(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment