首页 > 编程语言 >Delphi 用程序实现自动的html操作

Delphi 用程序实现自动的html操作

时间:2022-09-19 23:00:52浏览次数:66  
标签:begin end 程序实现 Delphi TObject html Exit False procedure

unit Unit1;   interface   uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, OleCtrls, SHDocVw, mshtml, StdCtrls, ExtCtrls;   const       CMySearchName: string = 'test';   type   TForm1 = class(TForm)     WebBrowser1: TWebBrowser;     ButtonSearch: TButton;     ButtonRefresh: TButton;     TimerRefresh: TTimer;     TimerSearch: TTimer;     ButtonIsFind: TButton;     TimerFind: TTimer;     TimerDial: TTimer;     ButtonDial: TButton;     procedure ButtonRefreshClick(Sender: TObject);     procedure ButtonSearchClick(Sender: TObject);     procedure WebBrowser1NavigateComplete2(ASender: TObject;       const pDisp: IDispatch; var URL: OleVariant);     procedure WebBrowser1NavigateError(ASender: TObject; const pDisp: IDispatch;       var URL, Frame, StatusCode: OleVariant; var Cancel: WordBool);     procedure TimerRefreshTimer(Sender: TObject);     procedure TimerSearchTimer(Sender: TObject);     procedure ButtonIsFindClick(Sender: TObject);     procedure TimerFindTimer(Sender: TObject);     procedure FormShow(Sender: TObject);     procedure ButtonDialClick(Sender: TObject);     procedure TimerDialTimer(Sender: TObject);   private     FIsNavSuccess: Boolean;     FHasExcScript: Boolean;     FHasSearch: Boolean;     FHasFind: Boolean;   public     { Public declarations }   end;   var   Form1: TForm1;   implementation   {$R *.dfm}   procedure ExecuteScript(aWebBrowser: TWebBrowser; XScript: WideString; language: WideString = 'javascript'); var   HTDoc: IHTMLDocument2; begin   HTDoc := (aWebBrowser.Document as IHTMLDocument2);   if (HTDoc <> nil) then   begin       if HTDoc.parentWindow <> nil then         HTDoc.parentWindow.ExecScript(XScript, Olevariant(language)) ;   end; end;   procedure DoIdle(XMsSec: Cardinal); var   ElapsedTime: Cardinal; begin   ElapsedTime := 0;   while ElapsedTime < XMsSec do   begin     Application.ProcessMessages;     Sleep(10);     Inc(ElapsedTime, 10);   end; end;   procedure MoniClick(X, Y: Integer); var   LCount: Integer; begin   LCount := 0;   while not SetCursorPos(X, Y) do   begin     Inc(LCount);     if LCount > 100 then       Exit;   end;      DoIdle(100);   mouse_event(MOUSEEVENTF_LEFTDOWN, 0,0,0,GetMessageExtraInfo());   DoIdle(100);   mouse_event(MOUSEEVENTF_LEFTUP, 0,0,0,GetMessageExtraInfo()); end;     function GetBodyAll(XDoc: IDispatch): IHTMLElementCollection; var   LDoc: HTMLDocument;   LBody: HTMLBody; begin   Result := nil;        LDoc := XDoc as HTMLDocument;   if LDoc = nil then     Exit;   LBody := LDoc.body as HTMLBody;   if LBody = nil then     Exit;     Result := LBody.all as IHTMLElementCollection; end;   function GetBodyElement(const ABodyAll: IHTMLElementCollection; const AnElementName: string): IHTMLElement; var   LName: OleVariant;   LIndex: OleVariant; begin   Result := nil;     LName := AnElementName;   Result := ABodyAll.item(LName, LIndex) as IHTMLElement; end;   function GetBodyElementStrValue(XBodyAll: IHTMLElementCollection; const AnItemName: string; var RetStr: string): Boolean; var   LElem: IHTMLElement; begin   Result := False;     LElem := GetBodyElement(XBodyAll, AnItemName);   if LElem <> nil then   begin     try       RetStr := Trim(LElem.getAttribute('value', 0));       Result := True;     except     end;   end; end;   function GetIFrameBodyAll(XDoc: IDispatch; XFrameIndex: Integer): IHTMLElementCollection; var   LIframeCollection:IHTMLElementCollection;   L1Iframe:IWebBrowser;   LLen: Integer;     LDoc: HTMLDocument;   LBody: HTMLBody; begin   Result := nil;     LIframeCollection:=GetBodyAll(XDoc).tags('iframe') as IHTMLElementCollection;   LLen := LIframeCollection.length;   if (LLen > 0) and (XFrameIndex >= 0) and (XFrameIndex < LLen) then   begin     L1Iframe:= LIframeCollection.item(XFrameIndex, varEmpty) as IWebBrowser;     LDoc := L1Iframe.document as HTMLDocument;     if LDoc = nil then       Exit;     LBody := LDoc.body as HTMLBody;     if LBody = nil then       Exit;       Result := LBody.all as IHTMLElementCollection;   end; end;   function SetBodyElementStrValue(XBodyAll: IHTMLElementCollection; const AnItemName: string; const XValueStr: string): Boolean; var   LElem: IHTMLElement;   LValue: OleVariant; begin   Result := False;     LElem := GetBodyElement(XBodyAll, AnItemName);   if LElem <> nil then   begin     try       LValue := XValueStr;       LElem.setAttribute('value', LValue, 0);       Result := True;     except     end;   end; end;   procedure TForm1.ButtonSearchClick(Sender: TObject); const   CNameSearchName = 'J_SearchKeyword';   CNameBtn = 'J_SerachList'; var   LOldValue: string;   LBodyAll: IHTMLElementCollection;   LEdit: IHTMLInputElement;   LBtn: IHTMLElement; begin   inherited;     if not FIsNavSuccess then     Exit;     // 不模拟实现了   t1.focus(); t1.blur();   //MoniClick(Left + WebBrowser1.Left + 200, Top + WebBrowser1.Top + WebBrowser1.Height + 15);     //LBodyAll := GetIFrameBodyAll(WebBrowser1.Document, 0);   LBodyAll := GetBodyAll(WebBrowser1.Document);   if LBodyAll = nil then     Exit;     if FHasExcScript then   begin     LBtn := GetBodyElement(LBodyAll, CNameBtn);     if LBtn = nil then       Exit;       LBtn.click;       FHasSearch := True;     TimerFind.Enabled := True;       Exit;   end;     if not GetBodyElementStrValue(LBodyAll, CNameSearchName, LOldValue) then   begin     Exit;   end;   if LOldValue <> CMySearchName then   begin     if not SetBodyElementStrValue(LBodyAll, CNameSearchName, CMySearchName) then       Exit;       LEdit := GetBodyElement(LBodyAll, CNameSearchName) as IHTMLInputElement;     if LEdit = nil then       Exit;       ExecuteScript(WebBrowser1, ' var t1 = document.getElementById("J_SearchKeyword");  t1.focus(); t1.blur();');     FHasExcScript := True;   end; end;   procedure TForm1.FormShow(Sender: TObject); begin   TimerRefreshTimer(nil); end;   procedure TForm1.ButtonDialClick(Sender: TObject); var   LParent: HWND;   LHandle: HWND;   LRect: TRect; begin   //  class:   //  btn: tSkMainForm -> TConversationForm -> TNonLiveCallToolbar     LHandle := FindWindow('tSkMainForm', nil);   if LHandle = 0 then     Exit;     if not ShowWindow(LHandle,SW_SHOWNORMAL) then     Exit;   if not SetForegroundWindow(LHandle) then     Exit;     DoIdle(100);     LParent := LHandle;   LHandle := FindWindowEx(LParent, 0, 'TConversationForm', nil);   if LHandle = 0 then     Exit;     LParent := LHandle;   LHandle := FindWindowEx(LParent, 0, 'TNonLiveCallToolbar', nil);   if LHandle = 0 then     Exit;      if not GetWindowRect(LHandle, LRect) then     Exit;     MoniClick(LRect.Left + 50, LRect.Top + 22);     TimerDial.Enabled := False; end;   procedure TForm1.ButtonIsFindClick(Sender: TObject);     function IsFindIndex(XAllChild: IHTMLElementCollection; XIndex: Integer): Boolean;   var     LItem0: IHTMLElement;     LIndex0: OleVariant;     LName: OleVariant;     LFindText: WideString;     LSearchName: WideString;   begin     Result := False;     LIndex0 := XIndex;     // activity-item clearfix     LItem0 := XAllChild.item(LName, LIndex0) as IHTMLElement;     if LItem0 = nil then       Exit;       LFindText := LItem0.innerHTML;     LSearchName := CMySearchName;     if Pos(LSearchName, LFindText) > 0 then     begin       Result := True;     end;   end;      const   CNameActiveListName = 'J_ActivityList'; var   LBodyAll: IHTMLElementCollection;   LList: IHTMLElement;   LChild: IHTMLElementCollection;   I: Integer; begin   inherited;     if not FHasSearch then     Exit;     LBodyAll := GetBodyAll(WebBrowser1.Document);   if LBodyAll = nil then     Exit;     LList := GetBodyElement(LBodyAll, CNameActiveListName);   if LList = nil then     Exit;     LChild := LList.children as IHTMLElementCollection;   if LChild = nil then     Exit;     if LChild.length > 0 then   begin     for I := 0 to LChild.length - 1 do     begin       if IsFindIndex(LChild, I) then       begin         TimerFind.Enabled := False;         TimerRefresh.Enabled := False;         TimerSearch.Enabled := False;           FHasFind := True;         TimerDial.Enabled := True;           Break;       end;     end;   end; end;   procedure TForm1.ButtonRefreshClick(Sender: TObject); begin   FIsNavSuccess := False;   FHasExcScript := False;   FHasSearch := False;   FHasFind := False;     TimerSearch.Enabled := False;   TimerFind.Enabled := False;   TimerDial.Enabled := False;      WebBrowser1.Navigate('http://yingxiao.taobao.com/list.htm'); end;   procedure TForm1.TimerDialTimer(Sender: TObject); begin   if not TimerDial.Enabled then     Exit;   if not FHasFind then     Exit;   ButtonDial.Click; end;   procedure TForm1.TimerFindTimer(Sender: TObject); begin   if not TimerFind.Enabled then     Exit;   ButtonIsFind.Click; end;   procedure TForm1.TimerRefreshTimer(Sender: TObject); begin   if not TimerRefresh.Enabled then     Exit;   ButtonRefresh.Click; end;   procedure TForm1.TimerSearchTimer(Sender: TObject); begin   if not TimerSearch.Enabled then     Exit;   ButtonSearch.Click; end;   procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject;   const pDisp: IDispatch; var URL: OleVariant); begin   FIsNavSuccess := True;   TimerSearch.Enabled := True; end;   procedure TForm1.WebBrowser1NavigateError(ASender: TObject;   const pDisp: IDispatch; var URL, Frame, StatusCode: OleVariant;   var Cancel: WordBool); begin   FIsNavSuccess := False; end;   end.           object Form1: TForm1   Left = 0   Top = 0   BorderStyle = bsDialog   Caption = 'Form1'   ClientHeight = 552   ClientWidth = 930   Color = clBtnFace   Font.Charset = DEFAULT_CHARSET   Font.Color = clWindowText   Font.Height = -11   Font.Name = 'Tahoma'   Font.Style = []   OldCreateOrder = False   OnShow = FormShow   DesignSize = (     930     552)   PixelsPerInch = 96   TextHeight = 13   object WebBrowser1: TWebBrowser     AlignWithMargins = True     Left = 0     Top = 0     Width = 930     Height = 452     Margins.Left = 0     Margins.Top = 0     Margins.Right = 0     Margins.Bottom = 100     Align = alClient     TabOrder = 0     OnNavigateComplete2 = WebBrowser1NavigateComplete2     OnNavigateError = WebBrowser1NavigateError     ExplicitLeft = 3     ExplicitTop = 3     ExplicitWidth = 637     ExplicitHeight = 301     ControlData = {       4C0000001E600000B72E00000000000000000000000000000000000000000000       000000004C000000000000000000000001000000E0D057007335CF11AE690800       2B2E126208000000000000004C0000000114020000000000C000000000000046       8000000000000000000000000000000000000000000000000000000000000000       00000000000000000100000000000000000000000000000000000000}   end   object ButtonSearch: TButton     Left = 288     Top = 506     Width = 89     Height = 25     Anchors = [akLeft, akBottom]     Caption = 'ButtonSearch'     TabOrder = 1     Visible = False     OnClick = ButtonSearchClick   end   object ButtonRefresh: TButton     Left = 144     Top = 506     Width = 97     Height = 25     Anchors = [akLeft, akBottom]     Caption = 'ButtonRefresh'     TabOrder = 2     Visible = False     OnClick = ButtonRefreshClick   end   object ButtonIsFind: TButton     Left = 424     Top = 506     Width = 89     Height = 25     Caption = 'ButtonIsFind'     TabOrder = 3     Visible = False     OnClick = ButtonIsFindClick   end   object ButtonDial: TButton     Left = 560     Top = 506     Width = 75     Height = 25     Caption = 'ButtonDial'     TabOrder = 4     Visible = False     OnClick = ButtonDialClick   end   object TimerRefresh: TTimer     Interval = 50000     OnTimer = TimerRefreshTimer     Left = 8     Top = 464   end   object TimerSearch: TTimer     Enabled = False     Interval = 10000     OnTimer = TimerSearchTimer     Left = 40     Top = 464   end   object TimerFind: TTimer     Enabled = False     OnTimer = TimerFindTimer     Left = 72     Top = 464   end   object TimerDial: TTimer     Enabled = False     Interval = 5000     OnTimer = TimerDialTimer     Left = 112     Top = 464   end end      ————————————————   原文链接:https://blog.csdn.net/xiuzhentianting/article/details/48377259

标签:begin,end,程序实现,Delphi,TObject,html,Exit,False,procedure
From: https://www.cnblogs.com/tang-delphi/p/16709445.html

相关文章

  • HTML5
    HTML :HyperTextMarkupLanguage封装JS的库  Jqueryw3c 标准 万维网联盟结构化标准语言(HTMLXML)表现标准语言(CSS)行为标准(DOMECMAScript)......
  • HTML基础
    第二天HTML基础列表标签ul无序列表(只允许包含li标签)li可以包含任意内容 ol有序列表(只允许包含li标签)li可以包含任意内容 dl自定义列表(只允......
  • html2canvas把dom元素转换成图片时跨域,crossorigin="anonymous"导致跨域问题
    1.前端使用html2canvas是需要注意配置  crossorigin="anonymous"<divclass="c-l-c"ref="lz-image2"><imgclass="image":src="dataObj.matchFile.fileUrl+'?'......
  • Delphi WebBrowser完成网页自动登录
    订制一些WEB数据抓取项目时,会遇到需要先登录网站系统,这时候我们通常会做成由系统自动登录的方式。分析网页要完成这项功能,首先需要对项目的网页进行分析,找到关键数据项的......
  • Delphi实现HTMLWebBrowser实现HTML界面
    HTML的界面有以下特点:图文混排,格式灵活,可以包含Flash、声音和视频等,实现图文声像的多媒体界面,而且易于建立和维护。另外,HTML的显示环境一般机器上都具备,通常不需要安装额外......
  • 使用这 3 个未充分利用的 HTML 事件为您的网站添加一些风格
    使用这3个未充分利用的HTML事件为您的网站添加一些风格Photoby阿迪·戈德斯坦on不飞溅在网络上脱颖而出变得越来越困难,同时这样做也越来越重要。在这里,我收集......
  • 纯HTML、CSS制作Tab页面浏览(国外youtube分享)
    纯HTML、CSS制作Tab页面浏览(国外youtube分享)影片来源https://www.youtube.com/watch?v=oLqdy95LZSw&list=WL&index=72&t=259s近期执行一个小Project,主要是复制网页版......
  • index.html (Apache) 的正确缓存清除
    index.html(Apache)的正确缓存清除来自CompetaIT档案;BastiaanDressen于2018年2月22日首次发表。部署JavaScript应用程序的一种方法是使用一些构建工具(......
  • MiniProgramError {"data":"<!DOCTYPE html>\n<html lang=\"en\"&gt
    uniapp向后台发送带参数的post请求,在微信小程序运行出现如下错误:MiniProgramError{"data":"<!DOCTYPEhtml>\n<htmllang=\"en\">\n<head>\n<metacharset=\"utf-8\">\n<......
  • 将 HTML 转换为 Markdown 的快速 macOS 快捷方式
    将HTML转换为Markdown的快速macOS快捷方式关于如何将HTML转换为Markdown的快速教程。例如,在本教程结束时,您应该能够突出显示下面的文本……<h2>绝对网址</h2......