1、獲取網頁所有鏈接
var
elem: IHTMLElement;
coll: IHTMLElementCollection;
i: integer;
url, title: string;
begin
coll := (WebBrowser1.Document as IHTMLDocument2).all;
coll := (coll.tags('a') as IHTMLElementCollection);
for i := 0 to coll.Length - 1 do
begin // 循環取出每個鏈接
elem := (coll.item(i, 0) as IHTMLElement);
url := Trim(string(elem.getAttribute(WideString('href'), 0)));
title := elem.innerText;
ShowMessage(Format('鏈接標題:%s,鏈接網址:%s', [title, url]));
end;
end;
其他元素的獲取,方法類似
-----------------------------------
Delphi TWebBrowser[6] 獲取網頁所有鏈接(元素)、下拉菜單及GetElementByID返回值的有效性判定方法
https://blog.51cto.com/u_15069471/4024964
下拉菜單
uses MsHtml;
var
doc: IHTMLDocument2;
coll: IHTMLElementCollection;
iPos, iIndex: Integer;
selElem: IHtmlSelectElement;
optElem: IHtmlOptionElement;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
coll := doc.all.tags('select') as IHTMLElementCollection;
iPos := 0; //要訪問的下拉菜單的序號,從0開始為第一個
selElem := coll.item(iPos, 0) as IHtmlSelectElement;
if selElem = nil then Exit;
iIndex := 2; //下拉菜單的選項序號,從0開始為第一個,2為第三個選項
optElem := selElem.item(iIndex, 0) as IHtmlOptionElement;
if optElem = nil then Exit;
ShowMessage(optElem.text); //獲取該選項的值
optElem.selected := True; //選中該選項
end;
-----------------------------------
Delphi TWebBrowser[6] 獲取網頁所有鏈接(元素)、下拉菜單及GetElementByID返回值的有效性判定方法
https://blog.51cto.com/u_15069471/4024964
GetElementByID返回值有效性判定方法
var
aElement: OleVariant;
begin
aElement := WebBrowser1.OleObject.Document.GetElementByID('btnLogin');
if IDispatch(aElement) <> nil then //對返回值進行有效性檢查
begin
aElement.value := '登錄按鈕';
aElement.click;
end;
end;
-----------------------------------
Delphi TWebBrowser[6] 獲取網頁所有鏈接(元素)、下拉菜單及GetElementByID返回值的有效性判定方法
https://blog.51cto.com/u_15069471/4024964