locked
extracting links from Webbrowser control RRS feed

  • Question

  •            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

    • Moved by Wesley Yao Monday, August 24, 2009 3:22 AM not c++ specific (From:Visual C++ General)
    Thursday, August 20, 2009 10:18 PM

All replies

  • Unable to cast COM object of type 'mshtml.HTMLAreaElementClass' to interface type 'mshtml.IHTMLAnchorElement'
    You are casting apple to orange
    And why do you post in a C++ forum? you can get better help in an IE SDK newsgroup like microsoft.public.inetsdk.programming.webbrowser_ctl.

    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Thursday, August 20, 2009 10:45 PM
  • I tried posting in that forum and no one answers my questions. Can you just post a solution? post it in c++ if you like
    Thursday, August 20, 2009 11:38 PM
  • Posting here would be off-topic. I suggest you read the documentation of IHTMLDocument2::links Property in MSDN. If you still have question, post to the newsgroup, not here.


    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Thursday, August 20, 2009 11:52 PM