码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-06-18 00:33:39      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

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;

  

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

标签:

原文地址:http://www.cnblogs.com/tmdsleep/p/4584590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!