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

asked on

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

Hi guyz!

Here's the entire code of my project, as you see I use documentcomplete to fill it out and click the button. This works great. Now I'd like to take this a bit further > After the fields are filled and the button is clicked and it has loaded the new page, the documentcomplete should fire off a click on an image button instead of repeating  > So at button1 click it should fill in the fields, click the button after which it loads the new page and there it should click an image button. Should I use a timer or something to accomplish that?

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 fields and click the button
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
if FillForm(WebBrowser1, 'TopicTitle', 'Posted by Anonymale') = False then
    ShowMessage('Error. Field not available or no Form found.');
    if FillForm(WebBrowser1, 'TopicDesc', 'Test') = False then
    ShowMessage('Error. Field not available or no Form found.');
    if FillForm(WebBrowser1, 'Post', 'Imitation of automation!') = False then
    ShowMessage('Error. Field not available or no Form found.');
    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);
end;


procedure TForm1.WebBrowser1ProgressChange(Sender: TObject; Progress, ProgressMax: Integer);
begin
  if ProgressMax = 0 then
  begin
    label1.Caption := '';
    Exit;
  end;
  try
    if (Progress <> -1) and (Progress <= ProgressMax) then
      label1.Caption := IntToStr((Progress * 100) div ProgressMax) + '% loaded...'
    else
      label1.Caption := '';
  except
    on EDivByZero do Exit;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://myboard.com/index.php?act=Post&CODE=2');
  webbrowser1.SetFocus;
end;


Let me know if my information is insufficient ok?

My regards and max respect to all of you guyz!!

PeterdeB
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

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

If that image button is a part of a form and clicking it simply submits the form, you could submit that form without pressing the image:

WebBrowser1.OleObject.Document.SecondForm.submit;

where 'SecondForm' is the name of the form (if there is any, of course). If the form has no name you may still submit it based on its index:

WebBrowser1.OleObject.Document.forms.Item(0).submit; // submits the first form in the document

Regards, Geo
For pressing a named HTML button (including image buttons) I usually use the following code:

uses MSHTML_TLB, ComObj;

procedure PressHTMLButton(Browser:TWebBrowser; ElementName: String);
var
  WebDoc : IHTMLDocument2;
  pDispatch : IDISPATCH;
  elements : IHTMLElementCollection;
  Button: DispHTMLButtonElement;
begin
  // retreive the HTML document
  OleCheck(Browser.Document.QueryInterface(IID_IHTMLDocument2, WebDoc));
  // retreive all HTML elements
  elements := WebDoc.Get_all;
  // find an element with name ElementName:
  pDispatch := elements.item(ElementName, 0);
  // retreive the element
  OleCheck(pDispatch.QueryInterface(DIID_DispHTMLButtonElement, Button));
  // press the button
  Button.click;
end;

// usage
PressHTMLButton(wb, 'IMBtn');

You may create MSHTML_TLB unit by selecting 'Project-Import Type Library' from the menu and then selecing 'Microsoft HTML Object Library' and pressing 'Create Unit' button.

Regards, Geo

PS: By image button I mean a HTML element in a form like:
<input type="image" src="images/button1.gif" name="SubmitBtn" alt="Submit" border=0>
Avatar of PeterdeB

ASKER

Hi Geobul tnx in advance for your reply > I'm currently testing it and here's the code for the image button:

<a href="index.php?act=Post&amp;CODE=00&amp;f=2" title="Start een nieuw onderwerp"><img src='style_images/1/t_new.gif' border='0'  alt='Start new topic' />

My regards respect and appreciation!

PeterdeB

Ps how can I found out whether some page has a form to submit > search the source code for "form"?
Hi,

As far as I can see it's just a link not a submit button of a form. If that is correct then try to navigate the browser to that link directly after the page gets loaded:

if WebBrowser1.LocationURL = 'URL of the next page here' then begin
  try
    WebBrowser1.Navigate('http://myboard.com/index.php?act=Post&CODE=00&f=2');
  except
  end;
end;

Regards, Geo
Ops, wrong URL. Sorry. Should be:

WebBrowser1.Navigate('index.php?act=Post&amp;CODE=00&amp;f=2');
Sorry again. Something's wrong with me today.

WebBrowser1.Navigate(''http://myboard.com/index.php?act=Post&CODE=00&f=2');
Hey Geobul!

You are right with your assumptions about the button. To explain what Im trying to achieve >  My app assists in transferring messages from one forum to another since we failed to 'pass' the old database. Now I made it as easy as possible > I only have to:
1. copy the old message > 
2. click in my apps memo > 
3. press button of my app > 
4. copy old message 2 >
5. click in my apps memo > 
6. click the pseudo imagebutton (the url) > 
7. copy old message 3 >
8. click in my apps memo >
9. click the pseudo imagebutton(the url) >

The points are yours well ofcourse > tnx a lot!!

Regards and respect from PeterdeB
The pleasure was mine :-)

That image tag has no name and it's difficult to press it. If the link is being generated dynamicaly there is a way based on its index. Here is some code for navigating to a link in the current document:

uses MSHTML_TLB,  // Import type library 'Mictosoft HTML Object Library'
 ComObj;

procedure PressHTMLLink(Browser:TWebBrowser; Index: integer);
var
  WebDoc : IHTMLDocument2;
  pDispatch : IDISPATCH;
  elements : IHTMLElementCollection;
  Link: IHTMLLinkElement;
begin
  OleCheck(Browser.Document.QueryInterface(IID_IHTMLDocument2, WebDoc));
  elements := WebDoc.links;
  if elements.length > Index - 1 then begin
    pDispatch := elements.item(Index,0); //' Index' is a zero based index of the links
    OleCheck(pDispatch.QueryInterface(DispHTMLLinkElement, Link));
    Browser.Navigate(Link.href);
  end;
end;

PressHTMLLink(WebBrowser1, 2); // will navigate to the third link if there is one

Regards, Geo
Tnx a lot Geobul > copy & paste et voila > the links get clicked splendid a big thank you!!!

Regards all the way from Holland!

PeterdeB