Delphi 无法获取元素。。。webbrowser的元素。”A'和'INPUT'ando等等。。。德尔福2007
伙计们,我的英语很差,但我需要你们的帮助 我无法通过一个webbrowser获得帧和元素,我需要获得所有。德尔福2007 如果我的电脑上没有这个应用程序,我会得到所有,但当我安装这个应用程序时,许多输入都无法分配。看Delphi 无法获取元素。。。webbrowser的元素。”A'和'INPUT'ando等等。。。德尔福2007,delphi,input,browser,field,elements,Delphi,Input,Browser,Field,Elements,伙计们,我的英语很差,但我需要你们的帮助 我无法通过一个webbrowser获得帧和元素,我需要获得所有。德尔福2007 如果我的电脑上没有这个应用程序,我会得到所有,但当我安装这个应用程序时,许多输入都无法分配。看 public doc1: IHTMLDocument2; Elementos: IHTMLElementCollection; Elemento: IHTMLElement; end; procedure TNavegador.wbDocumentComplete(ASen
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事件将针对每个框架集+顶部文档触发。下面是一些如何正确实现此事件的示例代码:
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;
您必须处理每个框架集和顶部文档
编辑
由于OP没有线索,我做了一个小测试项目:
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标记:
@Tlama:这是一个很好的例子,OnDocumentcomplete将多次触发。您的问题在于,OnDocumentcomplete事件将针对每个框架集+顶部文档触发。下面是一些如何正确实现此事件的示例代码:
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;
您必须处理每个框架集和顶部文档
编辑
由于OP没有线索,我做了一个小测试项目:
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标记:
@特拉玛:这是一个很好的例子,OnDocumentcomplete将多次触发。我需要获取所有输入、帧、链接等,知道是谁做的。问题是您的wbDocumentComplete实现不正确,它被多次触发。@whosrdad,抱歉,但是你从哪里得知wbDocumentComplete被多次触发?我向你保证,你将为每个框架集获得此事件:@whosrdaddy,感谢你激励我尝试:我从未注意到,也从未需要你的答案是,那么我很可能是问题的原因+1我需要获取所有输入、框架、链接等,不知道是谁做的。问题是您的wbDocumentComplete实现不正确,它被触发了多次。@whosrdaddy,对不起,但是您从哪里得到的wbDocumentComplete被触发了多次?我向您保证,您将为每个框架集获得此事件:@whosrdaddy,感谢您鼓励我尝试:我从来没有注意到,也从来没有需要您的答案,那么我是谁最有可能是这里问题的原因+1请您进一步解释,什么是FOnCompleteDocLoaded和FOnFrameSetLoaded事件,以及如何处理它们中的解析以供OP使用?就我个人而言,我从未体验过OnDocumentComplete会因为导航而被多次触发,也许我只是运气好:谢谢这只是我当前实现的复制和粘贴。这是我的来源:。@user1213118这是否有用?不。。。我有一个问题。FOnCompleteDocLoaded和FOnFrameSetLoaded,这是什么?在此之后,我将在我的项目中使用whow。Elementos:=Doc.all.tags'A'作为IHTMlementCollection;是否为Doc修改Doc1?PS:Bryan Cryer在过去帮助我,许多电子邮件,好人,谢谢你。我只是不懂一件事。你为什么要反向阅读这些元素?为什么不简单地使用Index:=0 to Elements.length-1?请您进一步解释一下,什么是FOnCompleteDocLoaded和FOnFrameSetLoaded事件,以及如何在它们中处理OP的解析?就我个人而言,我从未体验过OnDocumentComplete会因为导航而被多次触发,也许我只是运气好:谢谢这只是我当前实现的复制和粘贴。这是我的来源:。@user1213118这是否有用?不。。。我有一个问题。FOnCompleteDocLoaded和FOnFrameSetLoaded,这是什么?在此之后,我将在我的项目中使用whow。Elementos:=Doc.all.tags'A'作为IHTMlementCollection;是否为Doc修改Doc1?PS:Bryan Cryer在过去帮助我,许多电子邮件,好人,谢谢你。我只是不懂一件事。你为什么要反向阅读这些元素?为什么不直接使用索引:=0到Elements.length-1?