• 模拟点击网页中的按钮 回复 "starcraft_ly" 的问题



    本例效果图:



    代码文件:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, OleCtrls, SHDocVw;
    
    type
      TForm1 = class(TForm)
        WebBrowser1: TWebBrowser;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      WebBrowser1.Align := alTop;
      Button2.Enabled := False;
      Button3.Enabled := False;
      Button4.Enabled := False;
      Button1.Caption := '模拟一个页面';
      Button2.Caption := '假如知道按钮名称';
      Button3.Caption := '假如知道按钮的 ID';
      Button4.Caption := '假如只知道是第几个按钮';
    end;
    
    {模拟一个页面}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      WebBrowser1.Navigate(
        'about:<head><title>标题</title><body bgcolor=#ff0000>' +
        '<form method="POST" action="http://del.cnblogs.com">' +
        '<input type="submit" value="提交" id="btnID" name="btnName">' +
        '</form></body>');
      Button2.Enabled := True;
      Button3.Enabled := True;
      Button4.Enabled := True;
    end;
    
    {假如知道按钮名称, 譬如是: btnName}
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      WebBrowser1.OleObject.document.all.item('btnName').click;
      //WebBrowser1.OleObject.document.all.item('btnName', 0).click;
      FormCreate(nil);
    end;
    
    {假如知道按钮的 ID, 譬如是: btnID}
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      WebBrowser1.OleObject.document.getElementByID('btnID').click;
      FormCreate(nil);
    end;
    
    {假如只知道是第几个按钮, 譬如是第一个}
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      WebBrowser1.OleObject.document.getElementsByTagName('input').item(0).click;
      FormCreate(nil);
    end;
    
    end.
    

    窗体文件:
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 152
      ClientWidth = 625
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      PixelsPerInch = 96
      TextHeight = 13
      object WebBrowser1: TWebBrowser
        Left = 8
        Top = 8
        Width = 360
        Height = 105
        TabOrder = 0
        ControlData = {
          4C00000035250000DA0A00000000000000000000000000000000000000000000
          000000004C000000000000000000000001000000E0D057007335CF11AE690800
          2B2E126208000000000000004C0000000114020000000000C000000000000046
          8000000000000000000000000000000000000000000000000000000000000000
          00000000000000000100000000000000000000000000000000000000}
      end
      object Button1: TButton
        Left = 8
        Top = 120
        Width = 148
        Height = 25
        Caption = 'Button1'
        TabOrder = 1
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 162
        Top = 119
        Width = 148
        Height = 25
        Caption = 'Button2'
        TabOrder = 2
        OnClick = Button2Click
      end
      object Button3: TButton
        Left = 316
        Top = 119
        Width = 148
        Height = 25
        Caption = 'Button3'
        TabOrder = 3
        OnClick = Button3Click
      end
      object Button4: TButton
        Left = 470
        Top = 120
        Width = 148
        Height = 25
        Caption = 'Button4'
        TabOrder = 4
        OnClick = Button4Click
      end
    end
    
  • 相关阅读:
    分别使用Nginx反向代理和Haproxy调度器实现web服务器负载均衡
    CentOS7.4 源码编译安装LNMP
    LVS-DR+keepalived高可用群集
    Weex 和 Web 平台的差异
    Weex 和 Vue 2.x 的语法差异
    如何将原有 Weex 项目改造成 Vue 版本
    Vue 2.x 在 Weex 和 Web 中的差异
    使用 Vuex 和 vue-router
    使用 Vue 开发 Weex 页面
    weex快速上手
  • 原文地址:https://www.cnblogs.com/del/p/1370907.html
Copyright © 2020-2023  润新知