Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have a form and one webbrowser now I want to retrieve data displayed on the webbrowser must do?

Div Tags:
HTML
<div id="divTop" >
  <div id="text-conent"
       style="width: 500px;
              float: right;" >
  </div>
  <div id="grid"
       style="margin-removed 505px;
              height: 700px;" >
  </div>
</div>

Code get data from webbrowser:
C#
HtmlAgilityPack.HtmlDocument doc;
string                       texts

doc = ( HtmlAgilityPack.HtmlDocument ) webBrowser1.
                                       Document.
                                       DomDocument;
texts = doc.DocumentNode.SelectSingleNode (
            "//div[@class='text-conent']/p]").InnerText;
richTextBox1.Text = texts;


Error:
Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to class type 'HtmlAgilityPack.HtmlDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID

I have to how to get the data in the correct webbrowser
Posted
Updated 22-Jun-15 5:21am
v3
Comments
Richard Deeming 22-Jun-15 12:13pm    
Further to gggustafson's answer, your example HTML has a <div> tag with an id of "text-conent", but your code is looking for a tag with a class of "text-conent". Your XPath query won't match any nodes until you fix it.

1 solution

HtmlAgilityPack.HtmlDocument is not compatible with mshtml.HTMLDocumentClass. Somewhere in your code, you probably point webBrowser1 to a file from which you want to extract the inner text of the div "text-conent" (are you sure of the spelling?).

I'd suggest that you follow the process that I used in SiteMapper Tool[^]
C#
HAP.HtmlDocument  web_page = new HAP.HtmlDocument ( );

try
    {
    WebRequest  web_request;
    WebResponse web_response;

    web_request = WebRequest.Create ( uri.AbsoluteUri );
    web_response = web_request.GetResponse ( );

    using ( Stream stream = web_response.
                            GetResponseStream ( ) )
        {
        web_page.Load ( stream );
        }
    }
catch ( WebException we )
    {
    node.Broken = true;
    continue;
    }


Simply use the HtmlAgilityPack Load method. I suggest that you never use the mshtml.HTMLDocumentClass.

Once that you have the HTML in web_page, you can extract the div inner text as you are currently doing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900