1

How to cast mshtml.IHTMLDivElement to mshtml.HTMLDivElementClass?

IHTMLElementCollection collection = doc.body.all;

foreach (var htmlElem in collection)
{
    if (htmlElem is mshtml.IHTMLDivElement)
    {
         mshtml.IHTMLDivElement div = htmlElem as mshtml.IHTMLDivElement;
         if (div != null)
         {
            //  HTMLDivElementClass divClass = (HTMLDivElementClass)div;  ?????????                      
         }
     }            
}

I need to access HTMLDivElementClass to be able to get all members of it.

enter image description here

2
  • Why do you need to do the cast? What's wrong with your commented out code? Commented Feb 10, 2012 at 15:41
  • @vcsjones Because I cannot access all properties I need inside HTMLDivElementClass. IHTMLDivElement has only 2 properites... For example I need to get ID of the DIV and etc... Commented Feb 10, 2012 at 15:44

1 Answer 1

1

Your code almost correct. Not sure what you want.

Please change your code like below

 mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all;

            foreach (var htmlElem in collection)
            {
                if (htmlElem is mshtml.IHTMLDivElement)
                {
                    mshtml.HTMLDivElementClass div = htmlElem as mshtml.HTMLDivElementClass;
                    if (div != null)
                    {
                    //DO YOUR CODE HERE 
                     //div.IHTMLElement_id
                    }
                }
            }

It is working for me and in "div" object is of the type "HTMLDivElementClass"

And one more suggestion if you want to all the DIV tag only from the page then use the following line of code.

mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.getElementsByName("div");

Instead of

 mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all;

That will remove your condition to check element is DIV or not.

Hope this help to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.