zoukankan      html  css  js  c++  java
  • 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;
    

      

  • 相关阅读:
    sqlalchemy访问Oracle数据库报错:UnicodeDecodeError: 'big5' codec can't decode byte 0xfb in position 2: illegal multibyte sequence
    Mac如何安装FastDfs
    Django执行Sql语句笔记
    跑DRF框架分页源码笔记
    Python Paginator分页学习
    Python Excel笔记
    npm run dev报错解决方法
    npm install --global vue-cli 报错 [..................] / rollbackFailedOptional: verb npm-session abfa82f3041ebc02
    MS17_010漏洞攻击Windows7
    虚拟机启动黑屏
  • 原文地址:https://www.cnblogs.com/tmdsleep/p/4584590.html
Copyright © 2011-2022 走看看