• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

How to adapt my code to decrease user interaction?

Hi guys! I wrote this small app which launches Internet Explorer in the FormCreate Event after which a button press fills in several fields and clicks a button to login to some site. So far so good.
Now I'd like to have this app launch IE >  do the button click > and close itself all by just running the application. (doubleclicking)
Now I've tried several scenario's but all of which were unsuccesful, I implemented some naps (sleep command) but I can't get this done.

Currently: Running the app > launches the desired IE Window
               Clicking the button > logs in automatically
               Closing the app > kills the app

Desired: Running the app > launches the desired IE Window > logs in > kills the app

How do I accomplish this 'behaviour'?

If you need to see my code please let me know!

My regards and max respect to all of you guys!!!

PeterdeB from Holland

Ps I can't figure out how to get this baby going and after a few hours of experimenting I gave up. Please provide working sample or assist in adapting my code.
0
PeterdeB
Asked:
PeterdeB
  • 3
  • 2
1 Solution
 
LRHGuyCommented:
I would suggest doing something with a timer. Drop a timer on the form, set the interval to 10 or 15 seconds, or however long it may take for IE to launch. In the timer event, the first time in call Button.Click, then wait another 10-15 seconds, on the second call, execute form.close.

Without seeing your code, we're working blind here!
0
 
PeterdeBAuthor Commented:
Ok tnx for telling me hang on and I'll post it!

PeterdeB
0
 
PeterdeBAuthor Commented:
Here you are. These are the procedures. Messing around with these resulted in either no fields that got filled, no button that got clicked or some java window popping up nagging because of the fact the fields were left blank while the button got clicked. After some hours I gave up.....tnx in advance!

PeterdeB

//Beginning code

procedure TForm1.FormCreate(Sender: TObject);
begin
ShellExecute(HANDLE,
    'open',
    'http://www.someurl.com/index.php',
    nil,
    nil,
    SW_SHOW);
end;


procedure TForm1.Button2Click(Sender: TObject);
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
 ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          // count forms on document and iterate through its forms
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            // iterate through elements
            for i := 0 to ovElements.Length - 1 do
            begin
              // when input fieldname is found, try to fill out
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'text') = 0) then
                begin
                  ovElements.item(i).Value := 'myname';
                end;
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'password') = 0) then
                begin
                  ovElements.item(i).Value := 'mypassword';
                end;
              except
              end;
              // when Submit button is found, try to click
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) and
                  (ovElements.item(i).Value = 'Log mij in') then  // Suchen für German
                begin
                  ovElements.item(i).Click;
                end;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;
0
 
LRHGuyCommented:
This works great for me.

I moved the code from button to Go1. Go1 returns TRUE if the 'click' button was found in the form. At startup, a two second timer starts. When it expires, it tries to fill in the form. If the CLICK was found it closes the program, otherwise it waits another two seconds and tries again. I leave the fine tuning to you!


procedure TForm2.FormCreate(Sender: TObject);
begin
ShellExecute(HANDLE,
    'open',
    'http://www.lrh.net/public/code/html-m/public/newuser.htm',
    nil,
    nil,
    SW_SHOW);
  Timer1.enabled:=true;
end;


function TForm2.Go1:boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
  result:=false; //assume no go
 ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          // count forms on document and iterate through its forms
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            // iterate through elements
            for i := 0 to ovElements.Length - 1 do
            begin
              // when input fieldname is found, try to fill out
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'text') = 0) then
                begin
                  ovElements.item(i).Value := 'custodian2';
                end;
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'password') = 0) then
                begin
                  ovElements.item(i).Value := 'mypassword';
                end;
              except
              end;
              // when Submit button is found, try to click
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) and
                  (ovElements.item(i).Value = 'Sign Up') then
                begin
                  ovElements.item(i).Click;
                  result:=true; // we're a go!
                end;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;



procedure TForm2.Timer1Timer(Sender: TObject);
begin
  Timer1.enabled:=false;
  if Go1 then begin
    close;
  end
  else begin
    Timer1.Enabled:=true; //start timer again
  end;
end;
0
 
PeterdeBAuthor Commented:
Brilliant truly brilliant Jiiieehaaaaaa!!!

Thanks pal thank you thank you!!!!!

regard from PeterdeB
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now