1

I have tried:

var browser1 = new WebBrowser();
browser1.Navigate("https://zikiti.co.il/");    
HtmlDocument document = browser1.Document;

But browser.Document is null.

Why?


What am I doing wrong ?

    public static void FillForm()
    {
        browser1 = new WebBrowser();
        browser1.Navigate(new Uri("https://zikiti.co.il/"));

        browser1.Navigated += webBrowser1_Navigated;
        Thread.CurrentThread.Join();
    }

    private static void webBrowser1_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
    {
        HtmlDocument document = browser1.Document;
        System.Console.WriteLine();
    }

The application is stuck. Btw, is there any easier way to fill and submit this form? (I cannot see the request header in Fiddler as the page is always blocked by JS).

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • See @Hans Passant's previous answer(s): http://stackoverflow.com/questions/4269800/c-webbrowser-control-in-a-new-thread/4271581#4271581 – BrokenGlass Sep 24 '11 at 02:21

2 Answers2

4

Because it takes time to download the html. The amount of time nobody ever wants to wait for, especially a user interface thread, the hourglass won't do these day.

It tells you explicitly when it is available. DocumentCompleted event.

You have to pump a message loop to get that event.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Actually you can also do this after the `Navigated` event which fires earlier; it's explicitly mentioned there. – Jon Sep 24 '11 at 00:24
  • No, that event only tells you that it *started* downloading. No valid Document property yet. – Hans Passant Sep 24 '11 at 00:28
  • Just tested it, `Document` is indeed valid after `Navigated`. It would be really bad documentation if it were not: *"When the Navigated event occurs, the new document has begun loading, which means you can access the loaded content through the Document, [...] properties."* – Jon Sep 24 '11 at 00:32
1

Because Navigate is asynchronous, and the navigation has not even started by the time you read the Document property's value.

If you look at the example on that page, you will see that to read the "current" URL it needs to subscribe to the Navigated event; same applies to reading Document. The documentation for this event states:

Handle the Navigated event to receive notification when the WebBrowser control has navigated to a new document. When the Navigated event occurs, the new document has begun loading, which means you can access the loaded content through the Document, DocumentText, and DocumentStream properties.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • @EladBenda: The first line of the documentation for `Navigated` says that it works the same when you do that as well. – Jon Sep 24 '11 at 00:25