[英]Obtaining visible text on a page from an IHTMLDocument2*
我正在嘗試獲取 Inte.net Explorer web 瀏覽器 window 的文本內容。
我正在執行以下步驟:
編輯
我的問題是
我嘗試了一種遞歸方法,但我對如何處理這樣的場景一無所知,
<div>
Hello World 1
<div style="display: none">Hello world 2</div>
</div>
在這種情況下,我將無法獲得“Hello World 1”
誰能幫我找出從 IHTMLDocument2* 中獲取文本的最佳方法。 我正在使用 C++ Win32,沒有 MFC,ATL。
謝謝,阿希什。
如果您在document.body.all
元素上向后迭代,您將始終從里到外地遍歷這些元素。 所以你不需要自己走遞歸。 DOM 會為你做那件事。 例如(代碼在 Delphi 中):
procedure Test();
var
document, el: OleVariant;
i: Integer;
begin
document := CreateComObject(CLASS_HTMLDocument) as IDispatch;
document.open;
document.write('<div>Hello World 1<div style="display: none">Hello world 2<div>This DIV is also invisible</div></div></div>');
document.close;
for i := document.body.all.length - 1 downto 0 do // iterate backwards
begin
el := document.body.all.item(i);
// filter the elements
if (el.style.display = 'none') then
begin
el.removeNode(true);
end;
end;
ShowMessage(document.body.innerText);
end;
旁注:至於您使用遞歸方法的場景:
<div>Hello World 1<div style="display: none">Hello world 2</div></div>
例如,如果我們的元素是第一個 DIV, el.getAdjacentText('afterBegin')
將返回"Hello World 1"
。 所以我們可能可以向前迭代元素並收集getAdjacentText('afterBegin')
,但這有點困難,因為我們需要為el.currentStyle.display
測試每個元素的父元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.