try
{
//HtmlElementCollection elemColl = null;
mshtml.IHTMLDocument2 HtmlDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument;
mshtml.IHTMLElementCollection links = HtmlDoc.links;
//IHTMLElementCollection forms = HtmlDoc.forms;
//foreach (IHTMLAnchorElementClass el in links)
foreach(IHTMLElement el in links)
{
//Debug.WriteLine(el.innerHTML);
mshtml.IHTMLAnchorElement anchorElement = (mshtml.IHTMLAnchorElement)el;
//Debug.WriteLine(el.href);
//listBox1.Items.Add(el.href);
Debug.WriteLine(String.Format("Href:{0}",anchorElement.href));
listBox1.Items.Add(String.Format("Href:{0}", anchorElement.href));
}
Counter++;
}
catch (Exception ee)
{
Debug.WriteLine("inside link extraction exception");
Debug.WriteLine(ee);
}
I am trying to get the links out of a web page with just the normal Windows.Forms.webBrowser control not using the ActiveX version this time. But when I run this and I think it should work I get the exception listed below and advice appreciated and code in either C++ or C# much appreciated also
Here is my exception:
A first chance exception of type 'System.InvalidCastException' occurred in Basic WebBrowser NO ACtiveX.exe
inside link extraction exception
System.InvalidCastException: Unable to cast COM object of type 'mshtml.HTMLAreaElementClass' to interface type 'mshtml.IHTMLAnchorElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1DA-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at Basic_WebBrowser_NO_ACtiveX.Form1.webBrowser1_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) in H:\Visual Studio 2008 websites and projects\Projects\Basic WebBrowser NO ACtiveX\Basic WebBrowser NO ACtiveX\Form1.cs:line 145