views:

313

answers:

4

Hi..

I am using Web browser control in a window form. Here i am navigating to some site with 1 parameter. It is loading the page into web browser but when i am looking for webbrowser.document to find some html tags so it is showing NULL for it. I want to find out All Anchor tags in webbrowse Loaded page. Following is my code.

 webChatPage.Navigate(ConfigurationManager.AppSettings["ServerURL"].ToString() + "/somepage.php?someparameter=" + sessionId);

 HtmlDocument hDoc = webChatPage.Document;  //hDoc = NULL in debugging               
 HtmlElementCollection aTag = hDoc.Links;
 MessageBox.Show(aTag.Count.ToString());

If there is any solution then help me out.

+1  A: 

You need to handle the Navigated event to be notified when the document has begun loading:

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.

EDIT: As BrianLy points out in the comments, a better solution would be to handle the DocumentCompleted event instead since at this point the document has finished loading. Your code will then be something like:

webChatPage.DocumentCompleted += (o, e) => {
    //called when document has finished loading
    HtmlDocument hDoc = webChatPage.Document;               
    HtmlElementCollection aTag = hDoc.Links; 
    MessageBox.Show(aTag.Count.ToString());
}

string url = ConfigurationManager.AppSettings["ServerURL"].ToString() + "/somepage.php?someparameter=" + sessionId;
webChatPage.Navigate(url);
Lee
It sounds to be me like he would be better waiting for the DocumentCompleted event so that the document is fully loaded. See http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx
BrianLy
A: 

Can u plz elaborate? I didn't get the answer... How this can be possible that page is loaded in web browser control and DocumentText shows null?? IN navigated event what should be notified??

Web pages load progressively. You need to use the provided events to be notified when the document has started to load, or is fully loaded. Your code to access the document executes immediately and you need to use the events to ensure you wait long enough for the document to be available.
BrianLy
A: 

You did not even wait for the Navigating event, so the navigation is not yet started when you try to access the document. Try wait until the document is downloaded.

Sheng Jiang 蒋晟
A: 

You mean to say, i should write HtmlDocument hDoc = webChatPage.Document; line in navigated event. M i correct???