我用TWebBrowser.Navigate打开一个页面(无框架)后,
怎么能取到页面上各元素的内容(不是要这个页面的源码)?
比如:我在打开的页面上取以下值:
文本内容;
按钮名称及标题
文字连接的显示内容及连接地址;
等等页面元素,
怎么能取到页面上各元素的内容(不是要这个页面的源码)?
比如:我在打开的页面上取以下值:
文本内容;
按钮名称及标题
文字连接的显示内容及连接地址;
等等页面元素,
调试欢乐多
var pDisp: OleVariant; var URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLElement;
I: Integer;
AnchorString: string;
begin
lstbxLinks.Clear;
// We will process the document at this time. Trying to do
// so earlier won't work because it hasn't fully loaded.
Doc := FInternetExplorer.Document as IHTMLDocument2;
if Doc = nil then
raise Exception.Create('Couldn''t convert the ' +
'FInternetExplorer.Document to an IHTMLDocument2');
// First, grab all the elements on the web page
ElementCollection := Doc.all;
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
// Next, check to see if it is a link (tagName will be A).
// You could easily find other tags (such as TABLE, FONT, etc.)
if HTMLElement.tagName = 'A' then
begin
// Now grab the innerText for this particular link. The innerText is
// all text that is inside of the particular tag. For example,
// this would give us "Go To Borland" from the HTML:
// <a href="http://www.borland.com"><b>Go To Borland</b></a>.
// If you want "<b>Go To Borland</b>" use innerHTML.
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
lstbxLinks.Items.Add(AnchorString);
end;
end;
end;http://lysoft.7u7.net
不知道Liu Yang兄知道不知道
如果找到的是按钮,如果用程序控制去点击这
个按钮?