var i:integer; t:OleVariant; begin t := WebBrowser1.Document; for i := 0 to t.all.length - 1 do begin if t.all.item(i).tagName = 'INPUT' then begin if t.all.item(i).type = 'submit' then begin t.all.item(i).click; exit; end; end; end; end;
oDoc := (Sender as TWebBrowser).Document; for I := 0 to oDoc.All.Length - 1 do begin oItem := oDoc.All.Item(I); if oItem.tagName = 'A' then begin vAttri := oitem.getAttribute('protocol', 0); //获得链接属性 if vAttri = 'http:' then begin vAttri := oitem.getAttribute('href', 0); if pos(('http://www.delphibbs.com', vAttri), vAttri) > 0 then //如果链接中包含地址 begin oitem.click; break; end; end; end; end;
窗体上放一个TwebBrowser控件uses MSHTM; procedure TForm1.Button1Click(Sender: TObject);var doc: IHTMLDocument2; all: IHTMLElementCollection; len, i, flag: integer; item: IHTMLElement; vAttri: Variant; begin //获得Webbrowser对象中的文档对象 doc := WebBrowser1.document as ihtmldocument2; //获得文档中所有的HTML元素集合 all := doc.get_all; len := all.get_length; //访问HTML元素集合中的每一个元素 for i := 0 to len - 1 do begin item := all.item(i, varempty) as IHTMLElement; //如果该元素是一个链接 if item.Get_tagname = 'A' then begin flag := 0; vAttri := item.getAttribute('protocol', flag); //获得链接属性 if vAttri = 'http:' then begin vAttri := item.getAttribute('href', flag); if pos('http://www.delphibbs.com', vAttri) > 0 then //如果链接中包含大富翁的地址 WebBrowser1.Navigate(vAttri );//点击这个链接 end; end; end; end;
procedure TForm1.Button2Click(Sender: TObject); var E,I:IHTMLElement; ID: string; begin ID := '你能知道的ID'; //<img id="ID" src="\Images\thefile.gif" align="absbottom"> //就是这里的ID //I:= I := (WebBrowser1.Document as IHTMLDocument2).images.item(ID,0) as IHTMLElement; //两个都试一下 E := (WebBrowser1.Document as IHTMLDocument2).all.item(ID,0) as IHTMLElement; if Assigned(E) then E.onclick; if Assigned(I) then I.onclick; end;
i:integer;
t:OleVariant;
begin
t := WebBrowser1.Document;
for i := 0 to t.all.length - 1 do
begin
if t.all.item(i).tagName = 'INPUT' then
begin
if t.all.item(i).type = 'submit' then
begin
t.all.item(i).click;
exit;
end;
end;
end;
end;
oItem := oDoc.All.Item(I);
if oItem.tagName = 'A' then begin
vAttri := oitem.getAttribute('protocol', 0); //获得链接属性
if vAttri = 'http:' then
begin
vAttri := oitem.getAttribute('href', 0);
if pos(('http://www.delphibbs.com', vAttri), vAttri) > 0 then //如果链接中包含地址
begin
oitem.click;
break;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);var
doc: IHTMLDocument2;
all: IHTMLElementCollection;
len, i, flag: integer;
item: IHTMLElement;
vAttri: Variant;
begin //获得Webbrowser对象中的文档对象
doc := WebBrowser1.document as ihtmldocument2;
//获得文档中所有的HTML元素集合
all := doc.get_all;
len := all.get_length;
//访问HTML元素集合中的每一个元素
for i := 0 to len - 1 do
begin
item := all.item(i, varempty) as IHTMLElement;
//如果该元素是一个链接
if item.Get_tagname = 'A' then
begin
flag := 0;
vAttri := item.getAttribute('protocol', flag); //获得链接属性
if vAttri = 'http:' then
begin
vAttri := item.getAttribute('href', flag);
if pos('http://www.delphibbs.com', vAttri) > 0 then //如果链接中包含大富翁的地址
WebBrowser1.Navigate(vAttri );//点击这个链接
end;
end;
end;
end;
其实我点击这个链接并不是要去连接网站,而是要WebBrowser1BeforeNavigate2传递一个参数。我的WebBrowser1上HTML代码是用<td align="right"><a href="L381">这是我要的显示内容</a></td>这样的形式。
var
E,I:IHTMLElement;
ID: string;
begin
ID := '你能知道的ID';
//<img id="ID" src="\Images\thefile.gif" align="absbottom">
//就是这里的ID
//I:=
I := (WebBrowser1.Document as IHTMLDocument2).images.item(ID,0) as IHTMLElement;
//两个都试一下
E := (WebBrowser1.Document as IHTMLDocument2).all.item(ID,0) as IHTMLElement;
if Assigned(E) then
E.onclick;
if Assigned(I) then
I.onclick;
end;