Search code examples
c#awesomiumdocument-ready

awesomium control isn't visiting google


Hy all,

I'm making an Winform project with an awesomium webcontrol inside an class. I'm navigating that controll to http://www.google.com/ ( just for the test ) and added an DocumentReady even listner to it. But it won't fire the listener... ( i don't get the "in the listener!" message)

Here's the code i've got ( i call the doSomeScrap in the main form (Form1):

class scrapper
{
    WebControl web = new WebControl();

    public void doSomeScrap()
    {
        MessageBox.Show("in the scrapper...");
        web.DocumentReady += webcontrolEventListener;
        web.Source = "http://www.google.com".ToUri();
        web.Update();
    }

    private void webcontrolEventListener(object sender, EventArgs e)
    {
        MessageBox.Show("in the listener!");
    }
}

Also, I've heard of LoadingFrameCompleted, but when i use that, i get the following error:

'Awesomium.Windows.Forms.WebControl' does not contain a definition for 'LoadingFrameCompleted' and no extension method 'LoadingFrameCompleted' accepting a first argument of type 'Awesomium.Windows.Forms.WebControl' could be found (are you missing a using directive or an assembly reference?)

So what did i do wrong? Or what did i forget to make this work?

Extra Info: I've got another webcontrol in the form gui, and when i use this code on it ( without the update() call, the controll is navigating... So my guys is that it isn't navigating because it isn't in the GUI. But how can i make it navigate then?


Solution

  • If you don't need to display the page being scraped to the user, I would perform web-scraping using a non-Windowed WebView that can execute on a background thread.

    Example of scraping in a non-windowed console app

    // Credit: Awesomium v1.7.2 C# Basic Sample
    //         by Perikles C. Stephanidis
    using System;
    using Awesomium.Core;
    using System.Threading;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace BasicSample
    {
        class Program
        {
            static void Main( string[] args )
            {
                WebCore.Initialize(WebConfig.Default);
    
                Uri url = new Uri("http://www.google.com");
    
                using ( WebSession session = WebCore.CreateWebSession(WebPreferences.Default) )
                {
                    // WebView implements IDisposable. Here we demonstrate
                    // wrapping it in a using statement.
                    using ( WebView view = WebCore.CreateWebView( 1100, 600, session ) )
                    {
                        bool finishedLoading = false;
                        bool finishedResizing = false;
    
                        Console.WriteLine( String.Format( "Loading: {0} ...", url ) );
    
                        // Load a URL.
                        view.Source = url;
    
                        // This event is fired when a frame in the
                        // page finished loading.
                        view.LoadingFrameComplete += ( s, e ) =>
                        {
                            Console.WriteLine( String.Format( "Frame Loaded: {0}", e.FrameId ) );
    
                            // The main frame usually finishes loading last for a given page load.
                            if ( e.IsMainFrame )
                                finishedLoading = true;
                        };
    
                        while ( !finishedLoading )
                        {
                            Thread.Sleep( 100 );
                            // A Console application does not have a synchronization
                            // context, thus auto-update won't be enabled on WebCore.
                            // We need to manually call Update here.
                            WebCore.Update();
                        }
    
                        // Print some more information.
                        Console.WriteLine( String.Format( "Page Title: {0}", view.Title ) );
                        Console.WriteLine( String.Format( "Loaded URL: {0}", view.Source ) );
                    } // Destroy and dispose the view.
                } // Release and dispose the session.            
    
                // Shut down Awesomium before exiting.
                WebCore.Shutdown();
    
                Console.WriteLine("Press any key to exit...");
                Console.Read();
            }
        }
    }
    

    Example of a working scraper using a windowed control

    using System;
    using System.Windows.Forms;
    
    namespace App
    {
        public partial class DemoForm : Form
        {
            private Awesomium.Windows.Forms.WebControl web;
    
            public DemoForm()
            {
                InitializeComponent();
                doSomeScrap();
            }
    
            public void doSomeScrap()
            {
                MessageBox.Show("in the scrapper...");
                web.DocumentReady += webcontrolEventListener;
                web.Source = new Uri("http://www.google.com");
                web.Update();
            }
    
            private void webcontrolEventListener(object sender, EventArgs e)
            {
                MessageBox.Show("in the listener!");
            }
    
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.web = new Awesomium.Windows.Forms.WebControl();
                this.web.Dock = System.Windows.Forms.DockStyle.Fill;
                this.Controls.Add(this.web);
    
                this.ClientSize = new System.Drawing.Size(800, 600);
                this.Name = "DemoForm";
                this.Text = "DemoForm";
    
                this.ResumeLayout(false);
            }
        }
    }