大悟还俗

邮箱 key_ok@qq.com 我的收集 http://pan.baidu.com/share/home?uk=1177427271
posts - 236, comments - 8, trackbacks - 0, articles - 0
  新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

Webbrowser中模拟连接点击(非鼠标模拟)

Posted on 2013-11-18 10:32 大悟还俗 阅读(...) 评论(...) 编辑 收藏
uses
  mshtml, ActiveX;

//初始加载网易主页
procedure TForm1.FormCreate(Sender: TObject);
begin
  Webbrowser1.Navigate('http://www.163.com/');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Document: IHTMLDocument2;
Element: IHTMLElement;
Anchors: IHTMLElementCollection;
sLink: string;
begin
   //查找网易新闻页面链接
   sLink := 'http://news.163.com/';
   Document := Webbrowser1.Document as IHTMLDocument2;
   if Assigned(Document) then
   begin
      Anchors := Document.Get_links;
      //遍历所有链接
      for i := 0 to Anchors.length - 1 do
      begin
         Element := Anchors.item(i, varempty) as IHTMLElement;
         //找到指定链接
         if Assigned(Element) and (UpperCase((Element as IHTMLAnchorElement).href) = UpperCase(sLink)) then
        begin
           //执行点击
           Element.Click;
           Break;
        end;
      end;
   end;
end;
View Code