首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

C# WebBrowser 操作巢狀 iframe:逐層定位文檔與元素

     編輯:程式設計綜合問答

結構

主文檔 → `iframe[name=tgb]` → 文檔 → `iframe[id=veditor1_Iframe]` → 文檔 → 編輯區 (可能是 `div[contenteditable]` 或 `body`)。

定位代碼

HtmlDocument mainDoc = webBrowser1.Document;
HtmlWindow frame1 = mainDoc.Window.Frames["tgb"];
if (frame1 != null) {
    HtmlDocument doc1 = frame1.Document;
    HtmlWindow frame2 = doc1.Window.Frames["veditor1_Iframe"];
    if (frame2 != null) {
        HtmlDocument doc2 = frame2.Document;
        // 若編輯區是 body
        HtmlElement body = doc2.Body;
        if (body != null) body.InnerText = "google";
        // 若是 contenteditable div
        // HtmlElement div = doc2.GetElementById("editorDiv");
        // if (div != null) div.InnerText = "google";
    }
}

關鍵點

  • 每層 `Frames` 集合用 `name` 或 `id` 索引。
  • 跨域 iframe 會拋出「拒絕訪問」異常,需同域或配置 `document.domain`。
  • QQ 空間編輯區常為 `div[contenteditable]`,用 `InnerText`/`InnerHtml` 賦值。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved