delphi - 如何使用embeddedwb检测简单网页上是否按下了按钮

标签 delphi dom mshtml

我在我的应用程序上使用embeddedwb,并且我有一个带有按钮的简单网页

<input name=mpi type=submit value=" Continue ">

我尝试过,但效果不好

if E.outerHTML = '<input name=mpi type=submit value=" Continue ">' then
begin
  if rLoginGate.IsConnectedToLoginGate then
  begin
    ToggleChatBtn;
  end;
end;

现在我想做的是,当我按下按钮时,我需要我的应用程序将其拾取并运行一个简单的命令,例如消息框,有人有任何想法吗?

谢谢

最佳答案

实现此目的的一种方法是使用 MSHTML.PAS 中的 HTML DOM 对象模型接口(interface)。

我之前的回答,在这里:Detect when the active element in a TWebBrowser document changes展示了如何通过 TWebBrowser 的 Document 对象访问它。 TEmbeddedWB 也通过其 Document 对象提供访问。

该答案及其注释显示了如何捕获与文档中特定节点以及特定事件相关的事件。

当然,如果 HTML 在您的控制之下,您可以通过为您感兴趣的 HTML 节点提供可通过 DOM 模型轻松找到的 ID 或属性,从而使事情变得更容易。

下面显示了如何修改我链接的答案中的代码示例 将 OnClick 处理程序附加到特定元素节点:

procedure TForm1.btnLoadClick(Sender: TObject);
var
  V : OleVariant;
  Doc1 : IHtmlDocument;
  Doc2 : IHtmlDocument2;
  E : IHtmlElement;
begin
  //  First, navigate to About:Blank to ensure that the WebBrowser's document is not null
  WebBrowser1.Navigate('about:blank');

  //  Pick up the Document's IHTMLDocument2 interface, which we need for writing to the Document
  Doc2 := WebBrowser1.Document as IHTMLDocument2;

  //  Pick up the Document's IHTMLDocument3 interface, which we need for finding e DOM
  // Element by ID
  Doc2.QueryInterface(IHTMLDocument3, Doc);
  Assert(Doc <> Nil);

  //  Load the WebBrowser with the HTML contents of a TMemo
  V := VarArrayCreate([0, 0], varVariant);
  V[0] := Memo1.Lines.Text;
  try
    Doc2.Write(PSafeArray(TVarData(v).VArray));
  finally
    Doc2.Close;
  end;

  //  Find the ElementNode whose OnClick we want to handle
  V := Doc.getElementById('input1');
  E := IDispatch(V) as IHtmlElement;
  Assert(E <> Nil);

  //  Create an EventObject as per the linked answer
  DocEvent := TEventObject.Create(Self.AnEvent, False) as IDispatch;

  //  Finally, assign the input1 Node's OnClick handler
  E.onclick := DocEvent;
end;

PS:我使用 TEmbeddedWB 已经有一段时间了,可能有一种更直接的方法来做这些事情,因为在我停止使用它之后(在 D5 时代)它发生了很多变化。即便如此,您也不会浪费时间研究这些东西,因为 COM 事件适用于各种事物,而不仅仅是 HTML DOM 模型。

关于delphi - 如何使用embeddedwb检测简单网页上是否按下了按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092609/

相关文章:

javascript - 一个 javascript 文件可以包含在两个 HTML 文档中吗?

javascript - 霍根 JS IF 语句

javascript - 检测有效的 DOM 元素

internet-explorer - IE "per browser"资源管理器栏

c++ - 为什么 Microsoft 有 IHTMLDocument、IHTMLDocument2、...、IHTMLDocument8?

delphi - IDE 本地历史记录管理器的最大修订容量是多少?我该如何配置它(想要放大)?

windows - 从Delphi 6和WinXP到Delphi 2007和Vista/Win7如何处理窗体大小问题

vb6 - 如何在 VB6 中使用 MSHTML Parser 去除所有 HTML 标签?

delphi - 如何更改DBChart系列标记字体运行时?

mysql - SQL 和 Delphi : recursive mechanism for creating a tree from a table