Into a web page, how to enter a value into a form and execute an button

I need to programatically enter a webpage, fill in one value and execute one button. And then to parse the result into a string. Parsing i done before with indy component "IdHTTP".

The field to enter a value into looks like:

<input name="what" type="text" value="" maxlength="100" tabindex="1" class="textFields" />

And the button :

</script><noscript><a href="http://somestuff" border="0" width="176" height="135"></a></noscript>
Paer ToernellAsked:
Who is Participating?
 
Eddie ShipmanConnect With a Mentor All-around developerCommented:
Since that is using a GET action, you can also post the values like this by navigating the this URL:
(based on the values in my FormSubmit in post 24278590)
http://www.allabolag.se/?what=what+value&where=where+value
0
 
Geert GOracle dbaCommented:
0
 
BdLmCommented:
what action is defined once you input data in the inputbox?

have a look at the action and send those data to the server
0
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.

 
Paer ToernellAuthor Commented:
The complete form looks like this:

<form name="f_search" method="get" action="/">

            <table width="648" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td valign="top">
            <table width="430" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td style="font-weight:bold; color:#666666">VAD SÖKER DU?</td>
                <td>&nbsp;</td>
              </tr>

              <tr>
                <td><input name="what" type="text" value="" maxlength="100" tabindex="1" class="textFields" /></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td style="font-size:10px; color:#666666">ANGE BOLAG, PERSONNAMN, <a href="/branscher" style="font-size:10px; color:#555555; text-decoration: underline" tabindex="4">BRANSCH</a> ELLER FRITT SÖKORD&nbsp;&nbsp;</td>
                <td>&nbsp;</td>

              </tr>
              <tr>
                <td colspan="2"><img src="/img/green_1px.jpg" width="8px" height="18px" alt="" /></td>
              </tr>
              <tr>
                <td style="font-weight:bold; color:#666666">VAR?</td>
                <td align="left">&nbsp;</td>
              </tr>

              <tr>
                <td><input name="where" type="text" value="" maxlength="100" tabindex="2" class="textFields" /></td>
                <td rowspan="2" valign="top"><input type="image" name="s" src="/img/sok.jpg" tabindex="3" /></td>
              </tr>
              <tr>
                <td style="font-size:10px; color:#666666">DU KAN KOMBINERA GATUADRESS, POSTNR, ORT, RIKTNR</td>
              </tr>
            </table>

          </td>
          <td>
                <div style="width:176px; height:135px; position:relative; float:left; display:inline; margin-right:4px;">
            <!--JavaScript Tag // Tag for network 552: SvD Nya Medier (SvD.se, Tasteline.com, E24.se, Jobb24.se, MinTur.se) // Website: Allabolag.se // Page: Allabolag // Placement: Bolagstipset (1927258) // created at: Feb 13, 2009 4:19:09 PM-->
            <script language="javascript"><!--
            document.write('<scr'+'ipt language="javascript1.1" src="http://adserver.adtech.de/addyn|3.0|552|1927258|0|2311|ADTECH;loc=100;target=_blank;key=;Grp=2986168350;misc='+new Date().getTime()+'"></scri'+'pt>');
            //-->
            </script><noscript><a href="http://adserver.adtech.de/adlink|3.0|552|1927258|0|2311|ADTECH;loc=300;key=;grp=2986168350" target="_blank"><img src="http://adserver.adtech.de/adserv|3.0|552|1927258|0|2311|ADTECH;loc=300;key=;grp=2986168350" border="0" width="176" height="135"></a></noscript>      
            <!-- End of JavaScript Tag -->
            </div>
          </td>

        </tr>
      </table>
      </form>


One related question is @ http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24355059.html

The answer i got was correct, but did not help me all the way. Still the output in the stream wont be the same result as if i did this manually with my webbrowser.
0
 
Paer ToernellAuthor Commented:
As i understand it the command :

TStringStream.Create(HTTPApp.HTTPEncode('what=' + 'anything' + '&'));

enters a valu into something like <input name="what" type="text" value="""

and everything is executed when the "post" command is given - pointing at the correct webpage.

what i don't understand is:

(1) Why is the name of the form never given.
(2) Why is the appropriate button not targeted.

Is there any working examples that for example enters something into Google and parses the answer out?
0
 
BdLmCommented:
sory can't  write some code for you :  
may be here is a litle help   http://delphi.about.com/od/twebbrowser/a/submit_web_form_2.htm 
0
 
Eddie ShipmanAll-around developerCommented:
You don't post enough of the HTML or show a URL for us to help figure the whole thing out. However, this code gets the instance of IE that has the form loaded, fills out the form fields named and posts to the form's action.

I don't know how to modify the FormSubmit call in Button1Click to make it work with your form but give it a shot.
procedure TForm1.Button1Click(Sender: TObject);
begin
  FormSubmit('GetOptions.aspx',  {FormAction}
             'Options Page',     {FormTitle}
             ['__VIEWSTATE', 'OptionsList', 'OptionsList', 'OptionsList'] {AFields},
             ['', '0', '0', '1'] {AFieldValues}
             );
end;
 
function TForm1.FormSubmit
  ( AFormAction:           String
  ; AFormTitle:            String
  ; AFields:               Array of String
  ; AFieldValues:          Array of String
  ) : Boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Form: IHTMLFormElement;
  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);
      btnElement := -1;
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        Document := WB.Document;
        // if correct instance...
        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
            Form := IDispatch(Document.Forms.Item(m)) as IHTMLFormElement;
            if Pos(UpperCase(AFormAction), UpperCase(Form.action)) > 0 then
            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') or
                     (UpperCase(ovElements.item(i).type) = 'IMAGE') then
                  begin
                      btnElement := i;
                  end;
                  if not ( (UpperCase(ovElements.item(i).type) = 'CHECKBOX') or
                           (UpperCase(ovElements.item(i).type) = 'RADIO') ) then
                  begin
                    if (LowerCase(ovElements.item(i).name) = LowerCase(AFields[i])) then
                    begin
                        ovElements.item(i).Value := AFieldValues[i];
                        inc(IEFieldsCounter);
                    end;
                  end
                  else
                  begin
                    if StrToInt(AFieldValues[i]) = 1 then
                    begin
                      ovElements.item(i).Checked := True;
                    end
                    else
                    begin
                      ovElements.item(i).Checked := False;
                    end;
                    inc(IEFieldsCounter);
                  end;
                except
                  // failed...
                end;
              end; { for i...}
              if btnElement > -1 then
              begin
                ovElements.item(btnElement).click;
              end
              else
              begin
                Document.forms.Item(m).submit;
              end;
            end;
          end;  { for m }
        end;  {if Pos(AFormTitle, ...}
      end;  { idoc <> nil }
    end; { wb <> nil }
  end;  { for k }
 
end;
 

Open in new window

0
 
BdLmCommented:
did you ask on the HTML function for the get command ? not sure to understand your post correctly

here is a  nice demo on HTML post and get

http://de.selfhtml.org/servercgi/cgi/formularverarbeitung.htm 
0
 
Paer ToernellAuthor Commented:
EddieShipman


I will look into the examples given by You and others. The URL with the complete code is "www.allabolag.se"
0
 
Eddie ShipmanAll-around developerCommented:
Ok, then, try this:



FormSubmit('http://www.allabolag.se/',  {FormAction}
           'allabolag.se - F&ouml;retagsinformation om alla Sveriges bolag',     {FormTitle}
           ['what', 'where'] {AFields},
           ['what value', 'where value'] {AFieldValues}
          );

Open in new window

0
 
Eddie ShipmanAll-around developerCommented:
Since the page is in a foreign language, I can't test it.
0
 
Eddie ShipmanAll-around developerCommented:
What values should be entered?
0
 
BdLmCommented:
all I know for twebbroser come sfrom thjat link  http://www.cryer.co.uk/brian/delphi/twebbrowser
0
 
Paer ToernellAuthor Commented:
Eddie, had a busy weekend - now some time. Thanx.

You can enter a Value like ' Volvo Personvagnar Aktiebolag' - or any Swedish company into the "what" field.

the result should be:

SÖKRESULTAT: 5 TRÄFFAR         
Volvo Personvagnar Aktiebolag       Göteborg
Visa på karta
Org.nummer: 556074-3089
Verksamhet: Motorfordonstillverkning
Volvo Personvagnar Norden Aktiebolag       Angered
Visa på karta
Org.nummer: 556413-4848
Verksamhet: Holdingverksamhet i icke-finansiella koncerner
Volvo Personvagnar Holding Aktiebolag       Stockholm
Visa på karta
Org.nummer: 556245-5567
Verksamhet: Holdingverksamhet i icke-finansiella koncerner
Ford VHC AB       Göteborg
Visa på karta
Org.nummer: 556566-7895
Bolaget är moderbolag till 'Volvo Personvagnar Holding Aktiebolag'
Blue Chip Jet Handelsbolag       Säve
Visa på karta
Org.nummer: 969639-1011
Verksamhet: Lufttransport, Passagerartrafik
Befattningshavare 'Volvo Personvagnar Aktiebolag'
0
 
Eddie ShipmanAll-around developerCommented:
What would go in the where field?
0
 
Eddie ShipmanAll-around developerCommented:
Ok, what do you want to do with the results?
0
 
Eddie ShipmanAll-around developerCommented:
It worked just fine when I used that value in the What field., I got the results just like you posted.
0
 
Paer ToernellAuthor Commented:
http://www.allabolag.se/?what=volvo
-
omg, that simple and it works! Thanx a lot, u saved my life!
0
 
Eddie ShipmanAll-around developerCommented:
Do you still want to parse the results?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.