【问题标题】:Click a button using delphi and javascript使用 delphi 和 javascript 单击按钮
【发布时间】:2017-03-29 21:12:04
【问题描述】:

我在使用 delphi 和 javascript 按下按钮时遇到了困难。我的问题与其他人的不同,因为其他人的问题是关于如何按下图像,并且它们与这篇文章的标题相同。另一个是关于铬的。我的问题是如何在我用 delphi 制作的网络浏览器中按下按钮,代码如下:

{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$WARNINGS ON}

unit FmDemo;

interface

uses
  SHDocVw, Controls, StdCtrls, Classes, OleCtrls, Forms;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    ComboBox1: TComboBox;
    procedure FormShow(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure ComboBox1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

uses
  SysUtils, Dialogs, MSHTML;

{$R *.dfm}

procedure TForm1.ComboBox1Change(Sender: TObject);
  { Make browser use selected font }
var
  Doc: IHTMLDocument2;      // current HTML document
  HTMLWindow: IHTMLWindow2; // parent window of current HTML document
  JSFn: string;             // stores JavaScipt function call
begin
  // Get reference to current document
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get parent window of current document
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  // Run JavaScript
  try
    JSFn := 'myFunction(''' + ComboBox1.Text + ''')';
    HTMLWindow.execScript(JSFn, 'JavaScript');
  except
    // handle exception in case JavaScript fails to run
    ShowMessage('Error running JavaScript');
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
  { Setup combo box and load document }
begin
  // Store screen fonts in combo box and disabled it
  ComboBox1.Items.Assign(Screen.Fonts);
  ComboBox1.Enabled := False;
  // Load the HTML page
  WebBrowser1.Navigate('C:\Users\Androide\Desktop\article-21-demo\CaseStudy\Test.html');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
  { Document loaded: enable combo box }
begin
  ComboBox1.Enabled := True;
end;

end.

我的html:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

当我加载页面时,永远不要按下按钮。我的目标是当我加载页面时,因为我的浏览器按下了这个按钮,例如(当我加载页面时从不按下按钮=:

never press the button

所以我必须手动按下..并告诉我你好世界以检查是否按下: hello word message when i press button

一些自动不按手动的方法。

参考:

http://delphidabbler.com/articles?article=21

【问题讨论】:

    标签: delphi


    【解决方案1】:

    您昨天发布了一个非常相似的 q,但在我发布此答案之前将其删除。

    它会按照您在删除的 q 中说的做,iirc 是调用 JavaScript 以从 Delphi 代码更改元素的字体。修改应该不难 做你想做的事,这似乎只是在使用组合框来选择新字体名称和使用 HTML 嵌入按钮方面有所不同。

    代码

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ExecScript(Memo2.Lines.Text);
    end;
    
    procedure TForm1.btnLoadFromMemoClick(Sender: TObject);
    var
      V : OleVariant;
      Unk : IUnknown;
    begin
      WebBrowser1.Navigate('about:blank');
      Doc2 := WebBrowser1.Document as IHTMLDocument2;
      Doc2.DesignMode := 'Off';
      V := VarArrayCreate([0, 0], varVariant);
      V[0] := Memo1.Lines.Text;
      try
        Doc2.Write(PSafeArray(TVarData(v).VArray));
      finally
        Doc2.Close;
      end;
    end;
    
    procedure TForm1.ExecScript(const Script: String);
    var
      Doc: IHTMLDocument2;
      HTMLWindow: IHTMLWindow2;
    begin
      Doc := WebBrowser1.Document as IHTMLDocument2;
      Assert(Doc <> Nil);
    
      HTMLWindow := Doc.parentWindow;
      Assert(HTMLWindow <> Nil);
      try
        HTMLWindow.execScript(Script, 'JavaScript');
      except
        // handle exception in case JavaScript fails to run
      end;
    end;
    

    Html(放入Memo1.Lines)

    <html>
      <head>
      <script type="text/javascript">
      function SetFont(fontname) {
        document.body.style.fontFamily = fontname;
      }
      </script>
      </head>
      <body>
      Something
      <br>
      <div>some more text</div>
      </body>
    </html>
    

    JavaScript(放入 Memo1.Lines)

    SetFont("Courier New");
    

    以下代码展示了如何使用 Delphi 代码单击一个 HTML 按钮其 id 为 'mybutton'

    var
      Doc3: IHTMLDocument3;
      E : IHtmlElement;
    begin
      Doc3 := WebBrowser1.Document as IHTMLDocument3;
      Assert(Doc3 <> Nil);
      E := Doc3.GetElementByID('mybutton');
      Assert(E <> Nil);
      E.click;
    end;
    

    但现在我已经告诉你如何从 Delphi 代码中调用字体更改 JavaScript,也许你不再需要单击 HTML 按钮了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2014-03-03
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多