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

时间:2016-01-29 19:34:48

标签: 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;

现在我想做的是当我按下按钮我需要我的应用程序来接它并运行一个简单的命令就像一个消息箱任何人都有任何想法?

感谢

1 个答案:

答案 0 :(得分:3)

执行此操作的方法是使用MSHTML.PAS中的HTML DOM对象模型接口。

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

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

当然,如果HTML在您的控制之下,您可以通过提供您对ID或属性感兴趣的HTML节点,让您自己更轻松,通过DOM模型。

以下显示了如何修改链接答案中的代码示例 将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模型。

相关问题