Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Connecting to Internet Explorer instance and getting form values

Posted on 2004-10-05
10
220 Views
Last Modified: 2010-04-05
Hi!

Anybody knows how to connect to an open Intenet Explorer instance and get its forms' values. (Not using a TWeBrowser component).

Thanks.
0
Comment
Question by:nany
10 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12228226
Would be difficult if you have multiple IE instances... :-)
0
 

Author Comment

by:nany
ID: 12229721
Just one
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12231395
You can get you to the IHTMLDocument2 of each instance.

See this example: http://www.euromind.com/iedelphi/ie5tools/ieautomate.htm

Then you can iterate the Document's elements and obtain the values.

Now, if you want to get what the user has put in BEFORE submitting the form,
could be another story because I don't think the values are modified in the document
at all but sent via the GET or POST.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

Expert Comment

by:NoCatter
ID: 12233170
I was just about to post the exact same question.
 I tried out http://www.delphi-source.de/tipps/LAN_Internet/?id=191 and modified it to get the value from a form. But the process consumption went through the roof (a constant 100%) and I kept getting an error (Automation 'form' object not possible)?!?, or something along those lines.

Anyway to cut a long story short, it kept going wrong.

If I find the solution some time i'll email it to you if you want, my email is jgreen_188{a.t}hotmail.com ({a.t} = @).
Send me a email so I know your e-mail address.

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12239196
I took the code on the site you just mentioned and modified it to work with any form AND without using the stupid ExecuteScript function.

I tested this on a login for to an InvisionBoard that I maintain. This will work with any form that you have defined correctly in the
parameters. MAKE SURE you even include the hidden form variables, I had one and one radio and one check that I didn't care about.
You can not set the "value" for a radio or check, you have to use the Checked property. So this could be further modified to check for
radio and check types and set the checked property based upon the passed value.

This will not work, however, when you don't have a submit button. This is a case when you have an image that acts as a submit but calls
form.submit in it's onClick method. I am working on a modification to handle this as well as radio/checkboxes now


function FormSubmit(AFormTitle: String; AFields: Array of string; AFieldValues: array of string): Boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
  IEFieldsCounter: Integer;
  btnElement: 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;
        Document := WB.Document;
        // if correct document...
        if Pos(AFormTitle, Document.Title) <> 0 then
        begin
          // count forms on document and iterate through its forms
          IEFieldsCounter := 0;
          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
              try
              // if input fields found, try to fill them out
                if (UpperCase(ovElements.item(i).type) = 'SUBMIT') then
                begin
                    btnElement := i;
                end;
                if (LowerCase(ovElements.item(i).name) = LowerCase(AFields[i])) then
                begin
                    ovElements.item(i).Value := AFieldValues[i];
                    inc(IEFieldsCounter);
                end;
              except
                // failed...
              end;
            end; { for i...}
            if btnElement > -1 then
            begin
              ovElements.item(btnElement).click;
              btnElement := -1;
            end;
          end;  { for m }
        end; {if Pos(AFormTitle...}
      end;  { idoc <> nil }
    end; { wb <> nil }
  end;  { for k }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FormSubmit('Log In',                                                                               {FormTitle}
             ['referer', 'UserName', 'PassWord', 'CookieDate', 'Privacy', 'submit'], {AFields}
             ['', sUser, sPass, '', '', '']                                                                {AFieldValues}
             );
end;


0
 

Expert Comment

by:NoCatter
ID: 12263083
But what about getting the value from a form.

The problem with getting the value from an IE instance form field with the code above ^ is that you don't know when the user has finished typing in a form so you have to constantly get the value of the form.

But when you put the code in a timer to repeatedly get the value the program uses up all 100% of the processer. Which can get a bit anoying
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12263332
Another way would be to sink the OnNavigate2 event and examine the POST or GET vars. But you want to know what they are typing
in BEFORE the form is submitted, right?
0
 

Expert Comment

by:NoCatter
ID: 12263477
yeah, soz i am being anoying :D
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 12276175
Get the IE EventSink demo here: http://www.techvanguards.com/files/IE4SinkDemo.zip
Then modify to catch the OnNavigate2 event and get the vars.

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12359601
[quote]when you put the code in a timer to repeatedly get the value the program uses up all 100% of the processer[/quote]

Not if you place it in a separate thread.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 91
Process Javascript errors with Delphi TWebBrowser 1 174
LAN or WAN ? 11 95
DBCtrlGrid, Delphi, Scroll 7 26
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question