Link to home
Start Free TrialLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

asked on

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

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
Avatar of Hypoviax
Hypoviax
Flag of Australia image

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
Avatar of PeterdeB

ASKER

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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Hypoviax
Hypoviax
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
and get rid of those brackets next to vk_MENU (after mapvirtualkey) - my mistake

Hypoviax
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
I am glad i could help. If you need assistance with anything else related to this solution post it here

Regards,

Hypoviax