WebBrowser里网页根据文字判断来点击链接 无Name及ID时

uses  ActiveX, ComObj, MSHTML;

 

根据连接文字点击连接-    一般情况下的连接 

Procedure HTMLClinkByText(text:string;Wbr:TWebBrowser);
var
    doc:IHTMLDocument2;
    len,I:integer;
    item:OleVariant;
    all:IHTMLElementCollection;
begin
    doc:=Wbr.Document as IHTMLDocument2;
    all:=doc.Get_links;
    len:=all.length;
    for I:=0 to len-1 do
     begin
       item:=all.item(I,varempty);
       if pos (text,item.InnerText)>0 then      //判断连接文字包含内容
          begin
            Wbr.Navigate(item.href);
          end;
     end;
end;

 

使用的时候,要点击包含"联系"这两个字的链接

HTMLClinkByText('联系',WebBrowser1);

 

----------------------------------------------------------------------------------------------

Iframe 里面的连接点击


function   GetFrame(FrameNo: Integer): IWebbrowser2;
var
OleContainer: IOleContainer;
enum: IEnumUnknown;
unk: IUnknown;
Fetched: PLongint;
begin
while Webbrowser1.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;
if Assigned(Webbrowser1.document) then
begin
Fetched := nil;
OleContainer := Webbrowser1.Document as IOleContainer;
OleContainer.EnumObjects(OLECONTF_EMBEDDINGS, Enum);
Enum.Skip(FrameNo);
Enum.Next(1, Unk, Fetched);
Result := Unk as IWebbrowser2;
end
else
Result := nil;
end;

 

procedure TForm1.Button4Click(Sender: TObject);
var
doc,frm:IHTMLDocument2;
iw:IWebbrowser2;
i,j,len: integer;
all:IHTMLElementCollection;
item:OleVariant;
begin
doc:=Webbrowser1.Document as IHTMLDocument2;
for i:=0 to doc.frames.length-1 do
begin
iw:=GetFrame(i);
frm:=iw.Document as IHTMLDocument2;
all:=frm.Get_links;
   len:=all.length;
    for j:=0 to len-1 do
     begin
       item:=all.item(j,varempty);
       ListBox1.Items.Add(item.InnerText+' ~ '+item.href); //文字和相应的连接

       //根据上面那个Pos判断  item.InnerText  里包含你要点击的内容

      //然后  Webbrowser1.Navigate(item.href);  ................................
     end;
end;
end;

  

posted @ 2015-06-17 23:05  TMD睡觉  阅读(870)  评论(0编辑  收藏  举报