首页 > 其他分享 >网页事件的模拟

网页事件的模拟

时间:2023-04-17 20:39:48浏览次数:27  
标签:begin end nil IHTMLElementCollection Exit 事件 网页 模拟 Result


unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, mshtml, StdCtrls;
type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    ButtonLogin: TButton;
    ButtonRefresh: TButton;
    procedure ButtonRefreshClick(Sender: TObject);
    procedure ButtonLoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
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.ButtonLoginClick(Sender: TObject);
const
  CNameUserName = 'miniLogin_username';
  CNamePwd = 'miniLogin_pwd';
  CMyUserName = '[email protected]';
  CMyPwd = '11';
var
  LOldValue: string;
  LBodyAll: IHTMLElementCollection;
  LBtn: IHTMLElement;
begin
  inherited;
  LBodyAll := GetIFrameBodyAll(WebBrowser1.Document, 0);
  if LBodyAll = nil then
    Exit;
    
  if not GetBodyElementStrValue(LBodyAll, CNameUserName, LOldValue) then
  begin
    Exit;
  end;
  if LOldValue <> CMyUserName then
  begin
    if not SetBodyElementStrValue(LBodyAll, CNameUserName, CMyUserName) then
      Exit;
  end;
  if not GetBodyElementStrValue(LBodyAll, CNamePwd, LOldValue) then
  begin
    Exit;
  end;
  if LOldValue <> CMyPwd then
  begin
    if not SetBodyElementStrValue(LBodyAll, CNamePwd, CMyPwd) then
      Exit;
  end;
  LBtn := GetBodyElement(LBodyAll, 'message_LOGIN_IMMEDIATELY');
  if LBtn = nil then
    Exit;
  LBtn.click;
end;
procedure TForm1.ButtonRefreshClick(Sender: TObject);
begin
  WebBrowser1.Navigate('url');
end;
end.



标签:begin,end,nil,IHTMLElementCollection,Exit,事件,网页,模拟,Result
From: https://blog.51cto.com/u_16076050/6196288

相关文章

  • 基于Python程序模拟核酸检测寻找最优化方案
    本文中的数学建模问题来源于NKU的数学建模第二次实战演练,由于本次是我来进行程序的编写,故将代码与笔记记录在这里。问题提要现有800万市民报名参与核酸检测,如果对每人逐一进行检测,所需时间和检测能力都超过现实情况,所以拟采用混样检测(grouptesting)方式进行。先考虑混样规模为......
  • IE和Google浏览器事件传递参数不同
    左键标示,IE里面是1,Google里面是0,右键都是2.判断为左键,0或1都是<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><spanid="t-p"οnmοusedοwn="whichButtonDown(e......
  • js EventLoop事件循环机制
    1、js是单线程语言,其事件分为:同步任务和异步任务,异步任务分为宏任务与微任务;2、执行顺序为:同步任务(主流程任务)--->微任务 ---->宏任务先执行同步任务,遇到异步任务则放入任务队列中,等同步任务执行完毕,执行任务队列中的微任务,再执行宏任务...主线程从"任务队列"中读取事件,这......
  • C#模拟C++模板特化对类型的值的支持
    概述C++的模板相比于C#的泛型,有很多地方都更加的灵活(虽然代价是降低了编译速度),比如C++支持变长参数模板、支持枚举、int等类型的值作为模板参数。C++支持枚举、int等类型的值作为模板参数,为C++的静态多态编程提供了很好的帮助,比如根据枚举值编译期确定某个对象的行为策略等(下文......
  • python+playwright 学习-52 iframe 定位与操作元素,监听事件,执行JS脚本总结
    前言本篇全面总结关于iframe的定位,iframe上元素的操作(输入框,点击等),iframe上的事件监听与iframe上执行JS脚本的总结。iframe对象的定位定位iframe对象,总的来说有四种方法page.frame_locator(selector)通过page对象直接定位iframe对象,传selector选择器参数page.loca......
  • P1449 后缀表达式-模拟栈
    题目描述所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。如:3*(5-2)+7 对应的后缀表达式为:3.5.2.-*7.+@。在该式中,@ 为表达式的结束符号。. 为操作数的结束符号......
  • MATLAB代码:综合能源系统能源交易模拟与博弈
    MATLAB代码:综合能源系统能源交易模拟与博弈摘要:首先介绍了综合能源交易模拟的研究现状,从物理上介绍了多种能源生产、传输、使用的模型,从市场运营上介绍了市场参与者的经营模式以及目标函数。然后基于多代理仿真,结合设备和系统的运行约束并考虑用户的需求模型,采用Q-Learning......
  • DOM事件模型
    一、事件模型:1、事件的促发会经历从上到下的捕获阶段,再经历从下到上的冒泡阶段;2、addEventListener(type,fn,true/false)可设置第三个参数选择阶段;3、可以使用event.stopPropagation()阻止捕获/冒泡;二、事件委托:把一个或者一组元素的事件委托到它的父层或者更外层元素上。优......
  • pyqt5-事件
    1、介绍pyqt提供了事件机制使用户和程序进行交互。2、xxed方法组件的类中声明的类似xxed形式的方法,用于表示事件,使用时将其调用connect方法,指定处理方法。示例:xx.clicked.connect(self.opt)绑定组件对象的鼠标单击事件,如果触发,则调用opt方法3、xxEvent方法组件的类中声......
  • 原始java写的模拟HTTP请求 HttpsMethod
    原始java写的模拟HTTP请求packagecom.fc.utility;importjava.awt.image.BufferedImage;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importja......