Greetings fellow coders, I have read many tutorials, and tried many different code snippets but I have hit a brick wall and decided its time to give my brain a rest and see if other coders can find a solution.
with the push of a button I will log into a web site, do some tasks, then log out.
I got the log in no problem, however my wall is going to the next link...
The page is NOT waiting to load before continuing.
As the code is NOW it logs in and before logging i even completes it goes to the next link so log in actually fails.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ToolWin, ComCtrls, Menus, ActnMan, ActnCtrls, ActnMenus,
TabNotBk, jpeg, ExtCtrls, OleCtrls, SHDocVw, StdCtrls, PNGButton, mshtml,
Buttons, inifiles;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
Options1: TMenuItem;
opt_switch: TMenuItem;
opt_exit: TMenuItem;
Help1: TMenuItem;
hlp_about: TMenuItem;
Panel1: TPanel;
Image2: TImage;
Panel2: TPanel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Panel3: TPanel;
WebBrowser1: TWebBrowser;
CoolBar1: TCoolBar;
Animate1: TAnimate;
ToolBar1: TToolBar;
png_btn_home: TPNGButton;
png_btn_back: TPNGButton;
png_btn_forward: TPNGButton;
png_btn_stop: TPNGButton;
png_btn_refresh: TPNGButton;
URLs: TEdit;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
StaticText4: TStaticText;
StaticText5: TStaticText;
Button1: TButton;
cur_user: TEdit;
Button2: TButton;
TabSheet2: TTabSheet;
updates: TMemo;
Button3: TButton;
StaticText6: TStaticText;
procedure png_btn_homeClick(Sender: TObject);
procedure png_btn_backClick(Sender: TObject);
procedure png_btn_forwardClick(Sender: TObject);
procedure png_btn_stopClick(Sender: TObject);
procedure png_btn_refreshClick(Sender: TObject);
procedure WebBrowser1CommandStateChange(Sender: TObject;
Command: Integer; Enable: WordBool);
procedure FormCreate(Sender: TObject);
procedure FindAddress;
procedure WebBrowser1BeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
procedure opt_exitClick(Sender: TObject);
procedure WebBrowser1DownloadBegin(Sender: TObject);
procedure WebBrowser1DownloadComplete(Sender: TObject);
procedure URLsKeyPress(Sender: TObject; var Key: Char);
procedure Button1Click(Sender: TObject);
procedure WebBrowser1NavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
CurDispatch: IDispatch; {save the interface globally }
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
{ Load the animation from the AVI file in the startup directory.}
Animate1.FileName := ExtractFilePath(Application.ExeName) + 'cool.avi';
WebBrowser1.Navigate('http://MYSITE.com/');
end;
procedure TForm1.FindAddress;
var
Flags: OLEVariant;
begin
Flags := 0;
WebBrowser1.Navigate(WideString(Urls.Text), Flags, Flags, Flags, Flags);
end;
procedure TForm1.png_btn_homeClick(Sender: TObject);
begin
WebBrowser1.Navigate('MYSITE.com');
end;
procedure TForm1.png_btn_backClick(Sender: TObject);
begin
WebBrowser1.GoBack; //This will GoBack in the browser
end;
procedure TForm1.png_btn_forwardClick(Sender: TObject);
begin
WebBrowser1.GoForward; //This will go Forward
end;
procedure TForm1.png_btn_stopClick(Sender: TObject);
begin
WebBrowser1.Stop; //This will stop the browser
Animate1.Active := False; // Stop the avi and show the first frame
end;
procedure TForm1.png_btn_refreshClick(Sender: TObject);
var
vRefresh : OleVariant;
begin
vRefresh := 3;
WebBrowser1.Refresh2(OLEVariant(vRefresh));
end;
procedure TForm1.WebBrowser1CommandStateChange(Sender: TObject;
Command: Integer; Enable: WordBool);
begin
case Command of
CSC_NAVIGATEBACK: png_btn_back.Enabled := Enable;
CSC_NAVIGATEFORWARD: png_btn_forward.Enabled := Enable;
end;
end;
procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
begin
URLs.Text := URL;
end;
procedure TForm1.WebBrowser1DownloadBegin(Sender: TObject);
begin
Animate1.Active := True; //Play the avi from the first frame indefinitely
end;
procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
begin
Animate1.Active := False; // Stop the avi and show the first frame
end;
procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if CurDispatch = nil then
CurDispatch := pDisp; { save for comparison }
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if (pDisp = CurDispatch) then
begin
CurDispatch := nil; {clear the global variable }
end;
end;
procedure TForm1.URLsKeyPress(Sender: TObject; var Key: Char);
begin
if (Key=#13) then
begin
WebBrowser1.Navigate(URLs.Text);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var iDoc: IHtmlDocument2;
i: integer;
ov: OleVariant;
iDisp: IDispatch;
iColl: IHTMLElementCollection;
iInputElement: IHTMLInputElement;
accINI : TIniFile;
begin
accINI := TIniFile.Create(ExtractFilePath(Application.EXEName) + 'acc.ini');
WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, iDoc);
if not assigned(iDoc) then begin
ShowMessage(' !!!??? Nothing dowloaded ... ');
Exit;
end;
ov := 'INPUT';
IDisp := iDoc.all.tags(ov);
if assigned(IDisp) then begin
IDisp.QueryInterface(IHTMLElementCollection, iColl);
if assigned(iColl) then begin
for i := 1 to iColl.Get_length do begin
iDisp := iColl.item(pred(i), 0);
iDisp.QueryInterface(IHTMLInputElement, iInputElement);
if assigned(iInputElement) then begin
if iInputElement.Get_name = 'ctl00$ctl00$cpMain$cpMain$LoginBox$Email_Textbox'
//user account e-mail
then iInputElement.Set_value(accINI.ReadString('email','em1',''));
if iInputElement.Get_name = 'ctl00$ctl00$cpMain$cpMain$LoginBox$Password_Textbox'
// password
then iInputElement.Set_value(accINI.ReadString('password','psd1',''));
//Submit Button
if iInputElement.Get_name = 'dlb'
then iInputElement.Get_form.submit;
// Show the Titel of the currently active Webpage in the titlebar
Caption := Webbrowser1.OleObject.Document.Title
end;
end;
end;
end;
cur_user.Text := accINI.ReadString('email','em1','');
accINI.Free;
WebBrowser1.Navigate('http://MYOTHERSITE.com/'); // here is where my wall is... the above procedure "WebBrowser1NavigateComplete2" I can ot figure how to recall it and use it here to force a "wait til page fully loads"
end;
procedure TForm1.opt_exitClick(Sender: TObject);
begin
Application.Terminate;
end;
end.
Thank you all.