开发者

Web browser.navigate("www.somesite.com") Load page in window but Webbrowser.Document returns Null

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开发者_如何学JAVA;
 MessageBox.Show(aTag.Count.ToString());

If there is any solution then help me out.


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜