不能得到的元素...网页浏览器的元素。 “A”和“INPUT”安藤等等...... 2007年德尔福 [英] can't get Elements... elements of webbrowser. 'A' and 'INPUT' ando so on... delphi 2007

查看:201
本文介绍了不能得到的元素...网页浏览器的元素。 “A”和“INPUT”安藤等等...... 2007年德尔福的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家伙的我的英语不好,但我需要你的帮助......

guys my english is bad, but I need your help...

我不能由一个网页浏览器获得帧和元素,我需要得到所有。 德尔福2007年。

I can't get frames and elements by one webbrowser, and I need get all. "delphi 2007".

没有在我的电脑这个应用程序,我得到的所有,但是当我安装该应用程序,许多投入不进行分配。见...

without this application on my pc, I get all, but when I install this application, many inputs not be assigned. see...

public
 doc1: IHTMLDocument2;
 Elementos: IHTMLElementCollection;
 Elemento: IHTMLElement;
end;

procedure TNavegador.wbDocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL:  OleVariant);
var
 Z : Integer;
begin
 doc1 := (pDisp as IWebBrowser2).Document as IHTMLDocument2;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Memo3.Text := Wb.OleObject.Document.documentElement.innerText; //not assigned

 memo2.Text := doc1.body.innerHTML; // work. <-----

 Elementos := (doc1.all).tags('A') as IHTMLElementCollection; //not assigned

 if Assigned(Elementos) then
 begin
  for Z := 0 to Elementos1.length - 1 do
  begin
   Elemento := Elementos.Item(Z, 0) as IHTMLElement;
   if Assigned(Elemento) then
   begin
    if pos('/IMG/bt_voltar.gif', Elemento.innerHTML) > 0 then
    begin
     Elemento.Click; //click in link back
    end;
   end;
  end;
 end;

end;

  procedure TForm1.Button2Click(Sender: TObject);
  var
   Q : Integer;
   Elementos1: IHTMLElementCollection;
   Elemento1: IHTMLElement;
  begin
     Elementos1 := (doc1.all).tags('INPUT') as IHTMLElementCollection; //not assigned

     for Q := 0 to Elementos1.length - 1 do
     begin
      Elemento1 := Elementos1.Item(Q, 0) as IHTMLElement;
      if Assigned(Elemento1) then
      begin
       if Elemento1.getAttribute('name', 0) = 'Post_me' then
       begin
        Elemento1.setAttribute('value', '010203', 0);
       end;

       if Elemento1.getAttribute('name', 0) = 'btn_click' then
       begin
        Elemento1.Click;
       end;
      end;
     end;

   end;

function getAllInputs(doc: IHTMLDocument2): IHTMLElementCollection; //not assigned
var
 elementos: IHTMLElementCollection;
begin
 elementos := (doc.all).tags('input') as IHTMLElementCollection;
 result := elementos;
end;

function getAllLinks(doc: IHTMLDocument2): IHTMLElementCollection; //not assigned
var
 elementos: IHTMLElementCollection;
begin
 elementos := (doc.all).tags('A') as IHTMLElementCollection;
 result := elementos;
end;

许多理念?????等待着。

感谢的。

推荐答案

您的问题在于,该OnDocumentComplete事件将为每个框架+顶部文件被解雇。下面是一些示例code如何正确地实施这一事件:

Your problem lies in the fact that the OnDocumentComplete event will be fired for EACH frameset + the top document. Here is some sample code how to correctly implement this event:

procedure TFrm_browser.BrowserDocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);

var CurrentBrowser: IWebBrowser2;
    TopBrowser: IWebBrowser2;
    Doc :  IHTMLDocument2;

begin
  CurrentBrowser := pDisp as IWebBrowser2;
  TopBrowser := (ASender as TWebbrowser).DefaultInterface;
  if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
   begin
    Doc := CurrentBrowser.Document as IHTMLDocument2;
    if CurrentBrowser = TopBrowser then
     begin
      if Assigned(FOnCompleteDocLoaded) then
       FOnCompleteDocLoaded(Self, Doc);
     end
    else
     begin
      if Assigned(FOnFrameSetLoaded) then
       FOnFrameSetLoaded(Self, Doc);
     end;
   end;
end;

您必须处理每个框架和顶部的文档。

You must process each frameset and the top document.

修改

由于OP没有一个线索,我做了一个小testproject:

Since the OP does not have a clue, I made a small testproject:

unit Unit4;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
    procedure GetH3Tags(Doc :  IHTMLDocument2);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 WebBrowser1.Navigate('http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols');
end;

procedure TForm1.GetH3Tags(Doc: IHTMLDocument2);

var Elements: IHTMLElementCollection;
    Element : IHTMLElement;
    Index : Integer;

begin
 Elements := Doc.all.tags('h3') as IHTMLElementCollection;
 Index := Elements.length;
 while Index > 0 do
  begin
   Dec(Index);
   Element := Elements.item(Index, '') as IHTMLElement;
   Memo1.Lines.Add(Element.innerText);
  end;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var CurrentBrowser: IWebBrowser2;
    TopBrowser: IWebBrowser2;
    Doc :  IHTMLDocument2;

begin
  CurrentBrowser := pDisp as IWebBrowser2;
  TopBrowser := (ASender as TWebbrowser).DefaultInterface;
  if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
   begin
    Doc := CurrentBrowser.Document as IHTMLDocument2;
    if CurrentBrowser = TopBrowser then
     begin
      // get tags for top level document
       GetH3Tags(Doc);
     end
    else
     begin
      // get tags for each frameset
       GetH3Tags(Doc);
     end;
   end;
end;


end.

DFM文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 427
  ClientWidth = 899
  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 = 209
    Top = 0
    Width = 690
    Height = 427
    Align = alClient
    TabOrder = 0
    OnDocumentComplete = WebBrowser1DocumentComplete
    ExplicitLeft = 56
    ExplicitTop = 24
    ExplicitWidth = 300
    ExplicitHeight = 150
    ControlData = {
      4C00000050470000222C00000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 209
    Height = 427
    Align = alLeft
    Color = clHighlight
    TabOrder = 1
  end
end

此样本将获得该页面的所有H3标签:
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols

This sample will get all H3 tags from this page: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols

@Tlama:这是一个很好的例子OnDocumentcomplete会触发多次

@Tlama: this is a good example where OnDocumentcomplete will fire multiple times.

这篇关于不能得到的元素...网页浏览器的元素。 “A”和“INPUT”安藤等等...... 2007年德尔福的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆