现在的位置: 首页 > 综合 > 正文

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

2013年10月05日 ⁄ 综合 ⁄ 共 1136字 ⁄ 字号 评论关闭

Delphi

  1. uses  
  2.   mshtml, ActiveX;  
  3.   
  4. //初始加载网易主页  
  5. procedure TForm1.FormCreate(Sender: TObject);  
  6. begin  
  7.   Webbrowser1.Navigate('http://www.163.com/');  
  8. end;  
  9.   
  10. procedure TForm1.Button1Click(Sender: TObject);  
  11. var  
  12. I: Integer;  
  13. Document: IHTMLDocument2;  
  14. Element: IHTMLElement;  
  15. Anchors: IHTMLElementCollection;  
  16. sLink: string;  
  17. begin  
  18.    //查找网易新闻页面链接  
  19.    sLink := 'http://news.163.com/';  
  20.    Document := Webbrowser1.Document as IHTMLDocument2;  
  21.    if Assigned(Document) then  
  22.    begin  
  23.       Anchors := Document.Get_links;  
  24.       //遍历所有链接  
  25.       for i := 0 to Anchors.length - 1 do  
  26.       begin  
  27.          Element := Anchors.item(i, varempty) as IHTMLElement;  
  28.          //找到指定链接  
  29.          if Assigned(Element) and (UpperCase((Element as IHTMLAnchorElement).href) = UpperCase(sLink)) then  
  30.         begin  
  31.            //执行点击  
  32.            Element.Click;  
  33.            Break;  
  34.         end;  
  35.       end;  
  36.    end;  
  37. end;  

 

C#(点击网易页面“新闻”链接)

  1. foreach (HtmlElement element in webBrowser1.Document.Links)  
  2. {  
  3.     if (element.InnerText == "新闻")  
  4.     {  
  5.         element.InvokeMember("click");  
  6.         break;  
  7.     }  

抱歉!评论已关闭.