简体   繁体   中英

Load source HTML as string using CefSharp Wpf

I am trying to use the CefSharp WPF package to load a web page then access the HTML source of the web page I have loaded.

I am able to display the web page correctly, but I want to be able to see the HTML of the page.

I am able to call the following, but it displays the text in a temporary text file, however I want to see the source as a string ideally but would be ok if the file saved to disk.

_webBrowser.ViewSourceCommand.Execute(this);

This is my class

public partial class View: Window
{
    private CefSharp.Wpf.ChromiumWebBrowser _webBrowser; 
    public View()
    {
        InitializeComponent();

        var settings = new CefSettings();
        settings.PackLoadingDisabled = true;
        _webBrowser = new CefSharp.Wpf.ChromiumWebBrowser();
        BotBrowser.Children.Add(_webBrowser);
        _webBrowser.Address = "http://www.google.com/";
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        _webBrowser.ViewSourceCommand.Execute(this);
    }
}

Assuming your using the latest release version ( 43.0.1 ) then use the GetSourceAsync() method.

https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp/WebBrowserExtensions.cs#L158

ChromiumWebBrowser implements the IWebBrowser interface so you can use the extension method if you just want the source for the main frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM