Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi
I am developing a wpf application. The browser is embedded in wpf application using webbrowser control.
One of my requirement is to inspect element. Similar functionality is present in IE Developer tool , Firefox's firebug.
For Eg. If i click on any element like "Button" or "Link", it should give me details (id, tagName, Xpath) of selected object but it should not perform any action like POST or Navigate. I have referred this link

http://msdn.microsoft.com/en-us/library/ie/ms536913%28v=vs.85%29.aspx[^]

My current code is working for Basic HTML Page

C#
private void wpfWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {

            mshtml.HTMLDocument doc1 = this.wpfWebBrowser.Document as mshtml.HTMLDocument;
	    mshtml.HTMLDocumentEvents2_Event iEvent;
            iEvent = (mshtml.HTMLDocumentEvents2_Event)doc1;
            iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(iEvent_onclick);
            iEvent.onmouseup += new mshtml.HTMLDocumentEvents2_onmouseupEventHandler(iEvent_onmouseup);
	}

bool iEvent_onclick(mshtml.IHTMLEventObj pEvtObj)
        {
	    pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;        
            return false;
        }

private void wpfWebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            
            e.Cancel = true;
            
        }


Same code is not working for IFrames present in webpage. for eg. Gmail mailbox is developed using Iframes.
To test it for IFrames you have to register events for each iframe present in DOM. Because IE treats each Iframe as separate window. Here is my code for IFrames


C#
private void wpfWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {

            mshtml.HTMLDocument doc1 = this.wpfWebBrowser.Document as mshtml.HTMLDocument;
	    if (doc1.parentWindow.frames.length > 0)
            {
                List<mshtml.HTMLDocumentEvents2_Event> iFrameEventList = new List<mshtml.HTMLDocumentEvents2_Event>(doc1.parentWindow.frames.length);
                
                for (int index = 0; index < doc1.parentWindow.frames.length; index++)
                {
                    try
                    {
                        object refIndex = index;
                        mshtml.HTMLWindow2 win = doc1.parentWindow.frames.item(ref refIndex) as mshtml.HTMLWindow2;
                        if (win != null)
                        {
                            mshtml.HTMLDocument innerdoc = win.document as mshtml.HTMLDocument;
                            
                            mshtml.HTMLDocumentEvents2_Event iframeEventItem = (mshtml.HTMLDocumentEvents2_Event)innerdoc;
                            iFrameEventList.Add(iframeEventItem);
                            iFrameEventList[index].onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(iFrameEvent_onclick);
                            iFrameEventList[index].onmouseup += new mshtml.HTMLDocumentEvents2_onmouseupEventHandler(MainWindow_onmouseup);
                            
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);                        
                    }
                }
            }
	}


void MainWindow_onmouseup(mshtml.IHTMLEventObj pEvtObj)
        {
	    pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;          
        }

bool iFrameEvent_onclick(mshtml.IHTMLEventObj pEvtObj)
        {
            pEvtObj.cancelBubble = true; 
            pEvtObj.returnValue = false;
            return true;
        }


If i try to click on any HTML Element on Gmail loggedIn page, i can capture the event in "iFrameEvent_onclick" or "MainWindow_onmouseup" event handler.
But i cant stop the action performed. For eg. When i click on any received mail it will open the mail instead of that I want to stop that action.

Please help me in solving my problem or let me know if i am missing something.


Thank you
Bipin
Posted
Updated 27-Jul-12 6:55am
v2

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