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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sam HobbsRank #107 · 20.5K reputationPosted Sep 8, 2011, 9:19 PM EDT
Sreenath GRank #8,268 · 225 reputationPosted Sep 8, 2011, 9:31 PM EDT
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 HobbsRank #107 · 20.5K reputationPosted Sep 8, 2011, 10:53 PM EDT
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.