How to retrieve htmldocument of popwindow and apply events on it
-
jueves, 05 de julio de 2012 5:18
Hi,I am working in a project to automate IE. First we need to store all the user interactions and then we need to playback.I am facing problem with modalpopups to recognize and get the htmldocument. the html doc of modal popup has frames. Now I am able to identify the modal pop using window activation element using the below code.
private void StartMonitoringForWindowEvents()
{
Task.Factory.StartNew(() =>
{
AutomationEventHandler windowOpenedHandler = new AutomationEventHandler(OnWindowOpened);System.Windows.Automation.Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent, AutomationElement.RootElement,
TreeScope.Descendants, windowOpenedHandler);
});
}private void OnWindowOpened(object source, AutomationEventArgs eventArgs)
{
try
{
AutomationElement sourceElement = (AutomationElement)source;string message = string.Format(
"Automation.WindowOpened PID: {0}, Handle: {1}, Name:{2}",
sourceElement.Current.ProcessId,
sourceElement.Current.NativeWindowHandle,
sourceElement.Current.Name);
if (sourceElement.Current.ClassName == "Internet Explorer_AppFrame")
{
CheckForModalPopup();
}
//Debug.WriteLine(message);// for each created window, register to watch for it being closed
RegisterForClosedWindowEvent(sourceElement);
}
catch(Exception e)
{
}
}Now I am able to identify when popup comes. But is when retrieving the document from it. I have written the below code.
windowhwnd = UnManagedCode.FindWindowA("Internet Explorer_AppFrame", null);
if (windowhwnd != IntPtr.Zero)
ieserverhwnd = UnManagedCode.FindWindowEx(windowhwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
HTMLDocument fdoc = GetIEDocFrmHwnd(ieserverhwnd) as HTMLDocument;//using user32 dll, getting the document . code is not included
IHTMLFrameSetElement2 htmlfrmset;
IHTMLDocument2 fdoc1 = null;
IHTMLFrameSetElement frmsetelem;
HTMLIFrame iFrame;
IHTMLElement elemCFS = fdoc.getElementById("contentFrameset");
HTMLDocument WARFDoc = null;
int i = fdoc1.frames.length;
if (elemCFS != null)
{
frmsetelem = elemCFS as IHTMLFrameSetElement;iFrame = (elemCFS.document as HTMLDocument).getElementById("Frame1") as HTMLIFrame;
if (frmsetelem.rows == "*,0")
iFrame = (elemCFS.document as HTMLDocument).getElementById("Frame1") as HTMLIFrame;
else
iFrame = (elemCFS.document as HTMLDocument).getElementById("Frame2") as HTMLIFrame;
iFrame1 = fdoc.getElementById("Frame1") as HTMLIFrame;
WARFDoc = (fdoc.getElementById("Frame1") as HTMLIFrame).contentWindow.document as HTMLDocument;
}
else
iFrame = fdoc.getElementById("modalframe") as HTMLIFrame;WARFDoc = iFrame.contentWindow.document as HTMLDocument;
here is where it is throwing invalid cast exception. When I call iFrame.contentWindow is throwing invalid cast exception. If I keep separate button "GetPopdoc" and use the above code inside it, then iFrame.contentwindow won't throw any error. So when i click this button when popup is opened, I get the document.
But what I need is, when pop up occurs, it should automatically recognize and retrive the document.
Can you tell me the solution or workaround for this.
Swarnamala
- Editado Swarnamala jueves, 05 de julio de 2012 11:32

