internal HtmlWindowCollection(HtmlShimManager shimManager, IHTMLFramesCollection2 collection) { htmlFramesCollection2 = collection; this.shimManager = shimManager; Debug.Assert(NativeHTMLFramesCollection2 != null, "The window collection object should implement IHTMLFramesCollection2"); }
public static IHTMLDocument[] GetFrames(IHTMLDocument doc) { if (doc != null) { try { IHTMLDocument2 doc2 = doc as IHTMLDocument2; if (doc2 != null) { IHTMLFramesCollection2 frames = doc2.frames; if (frames != null && frames.length > 0) { List <IHTMLDocument> res = new List <IHTMLDocument>(); for (int i = 0; i < frames.length; i++) { object index = i; IHTMLWindow2 frame = frames.item(ref index) as IHTMLWindow2; IHTMLDocument temp = GetFrameDocument(frame); if (temp != null) { res.Add(temp); } } return(res.ToArray()); } } } catch { } } return(null); }
private IHTMLElement GetElementByValueOnce(string tagName, string attName, string attValue) { HTMLDocument document = ((HTMLDocument)IE.Document); IHTMLElement el = null; IHTMLElementCollection tags = document.getElementsByTagName(tagName); IEnumerator enumerator = tags.GetEnumerator(); while (enumerator.MoveNext()) { IHTMLElement element = (IHTMLElement)enumerator.Current; if (element.getAttribute(attName, 0).ToString().Contains(attValue)) { return(element); } } if (el == null) { IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)document.frames; try { object o = null; for (int i = 0; i < frames.length; i++) { o = i; IHTMLWindow2 fr = (IHTMLWindow2)frames.item(ref o); HTMLDocument doc = (HTMLDocument)fr.document; tags = doc.getElementsByTagName(tagName); enumerator = tags.GetEnumerator(); while (enumerator.MoveNext()) { IHTMLElement element = (IHTMLElement)enumerator.Current; bool go = false; try { go = element.getAttribute(attName, 0).ToString().Contains(attValue); } catch { } if (go) { return(element); } } } } catch (Exception ex) { string x = ex.Message; } } return(null); }
private void button1_Click(object sender, EventArgs e) { SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); int a = shellWindows.Count; string urls = ""; string htmlContent = ""; string textBox2 = this.textBox2.Text; if (a == 0) { this.groupBox2.Text = "请确认已打开浏览器窗口"; } int num = 0; foreach (SHDocVw.InternetExplorer ie in shellWindows) { //filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); string url = ie.LocationURL; if (url.Contains("http")) { num++; urls = urls + url + "\n"; if (num == int.Parse(textBox2)) { mshtml.IHTMLDocument2 htmlDoc = (mshtml.IHTMLDocument2)ie.Document; IHTMLFramesCollection2 frames = htmlDoc.frames; int len = frames.length; int i = 0; IHTMLWindow2 mainFrame = null; IHTMLWindow2 windowFrame = null; for (; i < len; i++) { IHTMLWindow2 frame2 = frames.item(i) as IHTMLWindow2; //if ("mainFrame".Equals(frame2.name)) { // htmlContent= frame2.document.body.innerHTML.ToString(); //} if (frame2.name != null && frame2.name.Contains("window")) { htmlContent = frame2.document.body.innerHTML.ToString(); windowFrame = frame2; //HTMLIFrame fe = (HTMLIFrame)frames.item(i).document; //fe.contentWindow.execScript("doShut()", "javascript"); //触发点击事件 //mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)htmlDoc.parentWindow; //frame2.execScript(";", "javascript");//使用JS } if (frame2.name != null && frame2.name.Contains("mainFrame")) { htmlContent = frame2.document.body.innerHTML.ToString(); mainFrame = frame2; } } mainFrame.execScript("showRHBillDetailForAuth('3785441458','1','15000096057223')"); //windowFrame.execScript("doShut()"); //string aa = htmlDoc != null ? htmlDoc.body.outerHTML.ToString() : "***Failed***"; //htmlContent = aa; //string aa = htmlDoc.body.innerHTML.ToString(); //htmlContent = aa; //触发点击事件 //mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)htmlDoc.parentWindow; //win.execScript("doSubmit()", "javascript");//使用JS } } } this.groupBox2.Text = urls; this.richTextBox1.Text = htmlContent; this.textBox1.Text = num.ToString(); //Form2 form2 = new Form2(); //form2.Show(); ////this.Close(); //this.Hide(); }
/// <summary> /// Private helper method for the constructors /// </summary> /// <param name="document2">IHTMLDocument2</param> /// <param name="contentWindow">WindowDetails</param> private void Init(IHTMLDocument2 document2, WindowDetails contentWindow) { this.document2 = document2; this.contentWindow = contentWindow; this.document3 = document2 as IHTMLDocument3; // Check what access method is needed for the document IHTMLDocument5 document5 = (IHTMLDocument5)document2; //compatibility mode affects how height is computed isDTD = false; try { if ((document3.documentElement != null) && (!document5.compatMode.Equals("BackCompat"))) { isDTD = true; } } catch (Exception ex) { LOG.Error("Error checking the compatibility mode:"); LOG.Error(ex); } Rectangle clientRectangle = contentWindow.WindowRectangle; try { IHTMLWindow2 window2 = (IHTMLWindow2)document2.parentWindow; //IHTMLWindow3 window3 = (IHTMLWindow3)document2.parentWindow; IHTMLScreen2 screen2 = (IHTMLScreen2)window2.screen; IHTMLScreen screen = window2.screen; if (parent != null) { // Copy parent values zoomLevelX = parent.zoomLevelX; zoomLevelY = parent.zoomLevelY; viewportRectangle = parent.viewportRectangle; } else { //DisableScrollbars(document2); // Calculate zoom level zoomLevelX = (double)screen2.deviceXDPI / (double)screen2.logicalXDPI; zoomLevelY = (double)screen2.deviceYDPI / (double)screen2.logicalYDPI; // Calculate the viewport rectangle, needed if there is a frame around the html window LOG.DebugFormat("Screen {0}x{1}", ScaleX(screen.width), ScaleY(screen.height)); //LOG.DebugFormat("Screen location {0},{1}", window3.screenLeft, window3.screenTop); LOG.DebugFormat("Window rectangle {0}", clientRectangle); LOG.DebugFormat("Client size {0}x{1}", ClientWidth, ClientHeight); int diffX = clientRectangle.Width - ClientWidth; int diffY = clientRectangle.Height - ClientHeight; // If there is a border around the inner window, the diff == 4 // If there is a border AND a scrollbar the diff == 20 if ((diffX == 4 || diffX >= 20) && (diffY == 4 || diffY >= 20)) { Point viewportOffset = new Point(2, 2); Size viewportSize = new Size(ClientWidth, ClientHeight); viewportRectangle = new Rectangle(viewportOffset, viewportSize); LOG.DebugFormat("viewportRect {0}", viewportRectangle); } } LOG.DebugFormat("Zoomlevel {0}, {1}", zoomLevelX, zoomLevelY); } catch (Exception e) { LOG.Warn("Can't get certain properties for documents, using default. Due to: ", e); } try { LOG.DebugFormat("Calculated location {0} for {1}", startLocation, document2.title); if (name == null) { name = document2.title; } } catch (Exception e) { LOG.Warn("Problem while trying to get document title!", e); } try { url = document2.url; } catch (Exception e) { LOG.Warn("Problem while trying to get document url!", e); } sourceLocation = new Point(ScaleX((int)startLocation.X), ScaleY((int)startLocation.Y)); destinationLocation = new Point(ScaleX((int)startLocation.X), ScaleY((int)startLocation.Y)); if (parent != null) { return; } try { IHTMLFramesCollection2 frameCollection = (IHTMLFramesCollection2)document2.frames; for (int frame = 0; frame < frameCollection.length; frame++) { try { IHTMLWindow2 frameWindow = frameCollection.item(frame); DocumentContainer frameData = new DocumentContainer(frameWindow, contentWindow, this); // check if frame is hidden if (!frameData.isHidden) { LOG.DebugFormat("Creating DocumentContainer for Frame {0} found in window with rectangle {1}", frameData.name, frameData.SourceRectangle); frames.Add(frameData); } else { LOG.DebugFormat("Skipping frame {0}", frameData.Name); } } catch (Exception e) { LOG.Warn("Problem while trying to get information from a frame, skipping the frame!", e); } } } catch (Exception ex) { LOG.Warn("Problem while trying to get the frames, skipping!", ex); } try { // Correct iframe locations foreach (IHTMLElement frameElement in document3.getElementsByTagName("IFRAME")) { try { CorrectFrameLocations(frameElement); } catch (Exception e) { LOG.Warn("Problem while trying to get information from an iframe, skipping the frame!", e); } } } catch (Exception ex) { LOG.Warn("Problem while trying to get the iframes, skipping!", ex); } }