?
Solved

How to use a TWebbrowser to post some stuff (code is almost complete)

Posted on 2004-11-07
9
Medium Priority
?
317 Views
Last Modified: 2010-04-05
Hi guyz!!

Using a TWebbrowser I fail to implement a button click right after some fields are filled in. This is the code(from swissdelphi) I use to fill in some fields and now I'd like to click a button in addition. The buttons sourcecode I dropped under the code segment of my app.



function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;
var
  i, j: Integer;
  FormItem: Variant;
begin
  Result := False;
  //no form on document
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //count forms on document
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        //when the fieldname is found, try to fill out
        if FormItem.Item(j).Name = FieldName then
        begin
          FormItem.Item(j).Value := Value;
          Result := True;
        end;
      except
        Exit;
      end;
    end;
  end;
end;


//When the document is complete try to fill out the field homepage with the url
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
if FillForm(WebBrowser1, 'TopicTitle', 'Experts Exchange Rockzz!') = False then
    ShowMessage('Error. Field not available or no Form found.');
    if FillForm(WebBrowser1, 'TopicDesc', 'Like H*ll!!') = False then
    ShowMessage('Error. Field not available or no Form found.');
    if FillForm(WebBrowser1, 'Post', 'They always do!') = False then
    ShowMessage('Error. Field not available or no Form found.');
  end;


This is the sourcecode from the page Im posting to. (the button I want to click).
<input type="submit" name="submit" value="Plaats een nieuw onderwerp" tabindex="7" class="button" accesskey="s" />


So can anyone implement a button click on that button after the fields are filling in? I managed to do so without a TWebbrowser but I fail to do so with a TWebbrowser. All points go to  him who completes this code. Let me know if you need some information more okay?

My regards and max respect!

PeterdeB
0
Comment
Question by:PeterdeB
  • 7
  • 2
9 Comments
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12519960
Accesskey??

I am not sure about this, but is an accesskey like a short-cut key? Like pressing ctrl-s or something?

If so i know how to click that button! However, i need to know whether my understanding is correct and what keys need to be pressed.

Regards,

Hypoviax
0
 

Author Comment

by:PeterdeB
ID: 12520045
Hi Hypoviax!

I have really no idea what it represents I only noticed that the button code differed from buttons which I COULD press in order to submit something. That's why I posted the buttons source code, I'm not familiar with it at all. Would you please post your answer then I'll test it right away to see if it works ok?

My regards and respect and tnx for replying!

PeterdeB
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12520299
Well, the trouble is if my theory is right then it is some form of shortcut key, in which case i don't really know what the combination is (ctrl-s, alt-s etc). If i know the combination then you can use keyboard events, in effect pressing that combination.

I will try some of the html to see if is what i think it is then i will post the code.

Regards,

Hypoviax
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:Hypoviax
ID: 12520322
I am right:

http://www.cs.tut.fi/~jkorpela/forms/accesskey.html

Ok try this then:

 keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), 0, 0 );  //S key down
 keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), KEYEVENTF_KEYUP, 0); //S key up

For this to work the webbrowser must not lose focus. In otherwords, the webbrowser must have focus when you call the procedure to click the button. (webbrowser1.setfocus; RUN CODE)

regards,

Hypoviax
0
 
LVL 5

Accepted Solution

by:
Hypoviax earned 1000 total points
ID: 12520338
unction FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;
var
  i, j: Integer;
  FormItem: Variant;
begin
  Result := False;
  //no form on document
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //count forms on document
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        //when the fieldname is found, try to fill out
        if FormItem.Item(j).Name = FieldName then
        begin
          FormItem.Item(j).Value := Value;
          Result := True;
        end;
      except
        Exit;
      end;
    end;
     keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), 0, 0 );  //S key down
     keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), KEYEVENTF_KEYUP, 0); //S key up
  end;
end;

Maybe in like that. You will have to test as i don't have the form ;-)

Regards,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12520372
Sorry hang on. You need to press alt-s not just s so the code is:

keybd_event(vk_MENU,  MapvirtualKey( vk_MENU), 0 ), 0, 0 );  //Alt key down
     keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), 0, 0); //S key down
keybd_event(vk_MENU,  MapvirtualKey( vk_MENU), 0 ), KEYEVENTF_KEYUP, 0);//alt up
     keybd_event(ord('S'),  MapvirtualKey( ord('S'), 0 ), KEYEVENTF_KEYUP, 0); //s up

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12520375
and get rid of those brackets next to vk_MENU (after mapvirtualkey) - my mistake

Hypoviax
0
 

Author Comment

by:PeterdeB
ID: 12521929
Mister Hypoviax!! You rock! I just posted my message to my board with a single button click jieeehaaahhh!!!

A very big thank you! I implemented the setfocus and right after that it succeeded!!

My regards and max respect!!

PeterdeB
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12527344
I am glad i could help. If you need assistance with anything else related to this solution post it here

Regards,

Hypoviax
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 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