Hi,
In WPF applicaiton how to get View Source for a page loaded in Webbrowser control
After navigating, Webbrowser.document is null.
How can I achieve this.
Hi,
In WPF applicaiton how to get View Source for a page loaded in Webbrowser control
After navigating, Webbrowser.document is null.
How can I achieve this.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Sep 9, 2011, 2:53 AM
In that code you have:
IServiceProvider lObjServiceProvider = (IServiceProvider)lObjWwbBwserDocument.Document.DomDocument;
In that, lObjWwbBwserDocument.Document would be Webbrowser.document. Are you sure it is null?
Since I don't know what you need to do, I still cannot help much. As I said, I think that most of that code is likely unnecessary. Without more information about what you need to do, the sample code does not help me help you.
As best as I can tell, you are not using WPF; you are using Windows Forms. Perhaps my article Introduction to Web Site Scraping will help.
Sreenath GPosted Sep 9, 2011, 1:31 AM
Here is the code I have tried out…But with this I am not getting actual source for different URLS…
WebBrowser WebBrowser1 = new WebBrowser();
void lFnLoadPage()
{
try
{
string lStrUrl = "http://www.google.com";
Uri lObjURI = new Uri(lStrUrl);
WebBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(lObjHandleWebserverDocument);
WebBrowser1.Navigate(lObjURI);
}
catch (Exception)
{
}
}
private void lObjHandleWebserverDocument(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
try
{
Guid lObjSID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
var lObjWwbBwserDocument = ((WebBrowser)sender);
IServiceProvider lObjServiceProvider = (IServiceProvider)lObjWwbBwserDocument.Document.DomDocument;
Guid lObjServiceGuid = lObjSID_SWebBrowserApp;
Guid lObjiid = typeof(SHDocVw.IWebBrowser2).GUID;
SHDocVw.IWebBrowser2 lObjmyWebBrowser2 = (SHDocVw.IWebBrowser2)lObjServiceProvider.QueryService(ref lObjServiceGuid, ref lObjiid);
mshtml.HTMLDocument lObjDocument = (mshtml.HTMLDocument)lObjmyWebBrowser2.Document;
string lStrSource = lObjDocument.documentElement.innerHTML;
((WebBrowser)sender).Dispose();
}
catch (Exception)
{
throw;
}
}
Sam HobbsPosted Sep 9, 2011, 1:19 AM