Wait for the WebBrowser control to load and run all the JavaScript.

using (var browser = new WebBrowser()) {
    browser.Navigate(location);

    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    while (browser.ReadyState != WebBrowserReadyState.Complete) {
        Application.DoEvents();
    }
    log.Info("READY " + browser.Document.GetElementById("main").InnerText);
}

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    WebBrowser wb = (WebBrowser)sender;
    log.Info("COMPLETED " + wb.Document.GetElementById("main").InnerText);
}

Both logs return an element in which JavaScript changes were not applied.

Now I have another WebBrowser in WinForms and it will load the page at startup. This time I also use the event DocumentCompleted, but I have no source javascripted.

form.webBrowser1.DocumentCompleted += (sender, e) => {
    log.Info("READY HIAAR" + form.webBrowser1.Document.GetElementById("main").InnerText);
};

Finally, I tried to get the source using the button. First I let WebBrowser load the page into WinForms, and then I click the button after 5 seconds :

form.dataButton.Click += (sender, e) => {
    log.Info("READY HIAAR" + form.webBrowser1.Document.GetElementById("main").InnerText);
};

Only the latter log.inforeturns the actual content that I need .

How can I catch an event when all the JavaScript has been loaded and launched? Pressing hits the target.

+5

All Articles