Webbrowser判斷網頁控件是否存在?

網頁代碼:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建網頁 1</title>
<script>
function aa(){
  alert("abc");
}
</script>
</head>

<body>

<form method="POST" action="--WEBBOT-SELF--">
	<p><input type="button" value="按鈕" name="B3" οnclick="aa()"></p>
</form>

</body>

</html>


delphi判斷網頁中是否存在某個名字的控件

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, MSHTML_TLB;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Math;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('c:\index.htm');
end;

procedure TForm1.Button1Click(Sender: TObject);
var b:IHTMLElement;
begin
  b:=(WebBrowser1.Document as IHTMLDocument3).getElementById('B3');
  if b=nil then exit;//這裏判斷網頁中是不是找到了這個按鈕,如果換成B4就不能執行下面的代碼了
  b.click;
end;

end.


 

發佈了51 篇原創文章 · 獲贊 43 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章