tags:

views:

324

answers:

2

how can i retrieve text from a web sites text boxes in delphi for example suppose i type ''tiger'' in google's search box how do i retrieve text from that search box would wm_gettext or getwindowtext work? i am using delphi 7

A: 

The simplest way would be by using a regular expression on the original HTML code from the page.

Workshop Alex
but can you do it on running instance of a public browser?please help!
Omair Iqbal
+3  A: 

Hi Omair, try this code.

Only works with internet explorer. tested in Windows Vista,IE8 y Delphi 2007.

Uses
SHDocVw,
mshtml;

procedure GetTextFromEditIExplorer(ListStr: TStringList);
var
  ShellWindow            : IShellWindows;
  Web_Browser            : IWebbrowser2;
  reg_Shell_window       : IDispatch;
  Dummy                  : IHTMLDocument2;
  ovElements             : OleVariant;
  Document               : Variant;
  WindowsCount           : Integer;
  ElementsCount          : Integer;
  FormsCount             : Integer;
begin
  ShellWindow := CoShellWindows.Create;  //Provides access to the collection of open Shell windows
  for WindowsCount := 0 to ShellWindow.Count do     //iterate through number of windows in the Shell windows collection
  begin
    reg_Shell_window := ShellWindow.Item(WindowsCount);       //Returns the registered Shell window for a specified index.
    if reg_Shell_window = nil then Continue;    //go to next reg window
    reg_Shell_window.QueryInterface(iWebBrowser2, Web_Browser);   // determines if an interface can be used with an object
    if Web_Browser <> nil then
    begin
      Web_Browser.Document.QueryInterface(IHTMLDocument2, Dummy);
      if Dummy <> nil then
      begin
        Web_Browser     := ShellWindow.Item(WindowsCount) as IWebbrowser2;
        Document        := Web_Browser;
          for FormsCount := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(FormsCount).elements;
            for ElementsCount := 0 to ovElements.Length - 1 do
            begin
              try
                if (CompareText(ovElements.item(ElementsCount).tagName, 'INPUT') = 0)  and  (CompareText(ovElements.item(ElementsCount).type, 'text') = 0) then
                ListStr.Add('Control Name ['+ovElements.item(ElementsCount).Name+']'+' Type  -> '+ovElements.item(ElementsCount).Type+'  ->  Value  ['+ovElements.item(ElementsCount).Value+']');
              except
                ListStr.Add('Error Reading element n° '+IntToStr(ElementsCount));
              end;
            end;
          end;
      end;
    end;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
List : TStringList;
begin
   List:=TStringList.Create;
   GetTextFromEditIExplorer(List);
   ShowMessage(List.Text);
end;

Edit :

Omar unfortunately there is no simple solution to your problem. This is because each browser uses a different interface to interact with the information.

Here are a couple of suggestions

  • Firefox uses XPCOM, you can research about that.
  • Try using DDE (dynamic data exchange).
  • You can use WatiN, is a .net library wich work with firefox and iexplorer. you can see this article to learn how interact with a .net Assembly in delphi win32.

Bye.

RRUZ
thanks rruz :) this code seems hard! is their an easier way? i am not saying i wont be able to use this code but ill have to learn a couple of new things like ole and stuff and secondly i want to catch edit boxes from firefox as well.
Omair Iqbal
please help!
Omair Iqbal
+1 nice answer RRUZ. I agree that there's no simple way to achieve this, particularly if multiple browsers are to be supported. It's a shame there are no shared hooks for this sort of thing.
Argalatyr
thanks,i have used dde to get url from browsers successfully but i coudnt find any help on how to use dde to retrieve text from webpage's text box. can you point to any example on using dde to accomplish this
Omair Iqbal