问题
I am using CefSharp v55.0 in my WinForm project. After the page is loaded, I want to get HTML code from it. And for that I am using this:
private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
test.ViewSource();
test.GetSourceAsync().ContinueWith(code =>
{
var html = code.Result;
});
}
}
And for the crosscheck, I am also calling test.ViewSource() method, to see that if GetSourceAsync method is getting the whole code or not.
Unfortunetely, codes are different. ViewSource is getting the whole code, but GetSourceAsync is not getting codes by javascript generated in the page.
Plase lead me a way to get source code of the page like ViewSource, or tell me how to capture this ViewSource method's temp file.
Cheers.
回答1:
Try this, it works for me:
public void showSource() // <<<<<<<<<<<<<<<<<<<<<<<<<< Call this function
{
Task ts = getSource();
}
private async Task getSource()
{
try
{
//
string source = await chromeBrowser.GetBrowser().MainFrame.GetSourceAsync();
//
string f = Application.StartupPath + "\\currentSource.txt";
//
StreamWriter wr = new StreamWriter(f, false, System.Text.Encoding.Default);
wr.Write(source);
wr.Close();
//
System.Diagnostics.Process.Start(f);
//
}
catch (Exception)
{
//Error !
}
}
回答2:
VB.Net:
Sub ShowSource()
Dim ts As task = getSource()
End Sub
Private Async Function getSource() As Task
Dim source As String = Await wb.GetBrowser().MainFrame.GetSourceAsync()
Dim f As String = Application.StartupPath + "/currentSource.txt"
Dim wr As StreamWriter = New StreamWriter(f, False, System.Text.Encoding.Default)
wr.Write(source)
wr.Close()
System.Diagnostics.Process.Start(f)
End Function
来源:https://stackoverflow.com/questions/43451312/getting-html-from-cefsharp-browser