CefSharp documentcompleted
Asked Answered
B

2

6

I am trying to use cefshar browser in C# winforms and need to know how I know when page completely loaded and how I can get browser document and get html elements,

I just Initialize the browser and don't know what I should do next:

  public Form1()
        {


            InitializeComponent();
            Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser("http://google.com");
            BrowserContainer.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

        }
Bordie answered 1/2, 2017 at 16:58 Comment(1)
Read the general usage guide, Google CefSharp general usage. It's on the wikiAppellee
S
17

CefSharp has a LoadingStateChanged event with LoadingStateChangedArgs.

LoadingStateChangedArgs has a property called IsLoading which indicates if the page is still loading.

You should be able to subscribe to it like this:

browser.LoadingStateChanged += OnLoadingStateChanged;

The method would look like this:

private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
    if (!args.IsLoading)
    {
        // Page has finished loading, do whatever you want here
    }
}

I believe you can get the page source like this:

string HTML = await browser.GetSourceAsync();

You'd probably need to get to grips with something like HtmlAgility to parse it, I'm not going to cover that as it's off topic.

Shelbashelbi answered 1/2, 2017 at 17:3 Comment(1)
Thanks this really helpfulBordie
V
-1

I ended up using:

using CefSharp;

wbAuthorization.AddressChanged += OnAddressChanged;

and

private void OnAddressChanged(
    object s,
    AddressChangedEventArgs e)
{
    if (e.Address.StartsWith(EndUri))
    {
        ResultUri = new Uri(e.Address);
        this.DialogResult = DialogResult.OK;
    }
}

EndUri is the final page I want to examine and ResultUri contains a string I want to extract later. Just some example code from a larger class.

Venusberg answered 27/7, 2022 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.