Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-25
19
Medium Priority
?
859 Views
Last Modified: 2013-11-23
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>
0
Comment
Question by:Paer Toernell
  • 9
  • 5
  • 4
  • +1
19 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24239639
0
 
LVL 8

Expert Comment

by:BdLm
ID: 24239701
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
 

Author Comment

by:Paer Toernell
ID: 24249909
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Paer Toernell
ID: 24258651
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
 
LVL 8

Expert Comment

by:BdLm
ID: 24267760
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
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24271873
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
 
LVL 8

Expert Comment

by:BdLm
ID: 24273303
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
 

Author Comment

by:Paer Toernell
ID: 24276967
EddieShipman


I will look into the examples given by You and others. The URL with the complete code is "www.allabolag.se"
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24278590
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
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24280589
Since the page is in a foreign language, I can't test it.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24280650
What values should be entered?
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 2000 total points
ID: 24280688
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
 
LVL 8

Expert Comment

by:BdLm
ID: 24283795
all I know for twebbroser come sfrom thjat link  http://www.cryer.co.uk/brian/delphi/twebbrowser
0
 

Author Comment

by:Paer Toernell
ID: 24289347
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
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24289889
What would go in the where field?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24298000
Ok, what do you want to do with the results?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24298008
It worked just fine when I used that value in the What field., I got the results just like you posted.
0
 

Author Closing Comment

by:Paer Toernell
ID: 31574515
http://www.allabolag.se/?what=volvo
-
omg, that simple and it works! Thanx a lot, u saved my life!
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24304390
Do you still want to parse the results?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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