#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Using TWebbrowser to enter text and click button


    Using TWebbrowser to access a web form with a few fields on it, and a couple of buttons.

    I want to enter some text into one of these form Fields, then click one of the buttons ('Search'), with delphi code accessing the page via TWebbrowser.

    I can access the complete HTML page source (is in a frame but i do a fool workaround to trick it into discarding the outer frame anyway). I know what the button and the field names are.

    Having Zero luck with this so far, need a direction to be pointed in if nothing more. Any Ideas?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0
    Very complicated... but here is a code.
    Have a simple form with a button and a WebBrowser component. In the WebBrowser: a simple button (named Submit) and an edit component. If you click on the Form1's Button1 component you simulate a simple click on the WebBrowser's Submit button.

    ...
    uses ...OleCtrls, SHDocVw, MSHTML;

    procedure TForm1.FormCreate(Sender: TObject);
    var Url, Flags, TargetFrameName, PostData, Headers: OleVariant;
    begin
    Url := ExtractFilePath(Application.ExeName) + 'Test.htm';
    WebBrowser1.Navigate2(Url, Flags, TargetFrameName, PostData, Headers);
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
    doc: IHtmlDocument2;
    i: integer;
    ov: OleVariant;
    disp: IDispatch;
    collection: IHTMLElementCollection;
    inputelement: HTMLInputImage;
    begin
    WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, doc);
    if not Assigned(doc) then
    begin
    Exit;
    end;
    ov := 'INPUT';
    disp := doc.all.tags(ov);
    if Assigned(disp) then
    begin
    disp.QueryInterface(IHTMLElementCollection, collection);
    if Assigned(collection) then
    begin
    for i := 1 to collection.Get_length do
    begin
    disp := collection.item(pred(i), 0);
    disp.QueryInterface(HTMLInputImage, inputelement);
    if Assigned(inputelement) then
    begin
    if inputelement.Name = 'submit then
    begin
    inputelement.Click;
    end;
    end;
    end;
    end;
    end;
    end;



    Here is the code of test.htm:

    <HTML>
    <HEAD>
    <meta content="JavaScript" name="vs_defaultClientScript">
    </HEAD>
    <body style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Verdana; TEXT-DECORATION: none">
    <form id="Form1" method="post">
    <INPUT id="oText" style="Z-INDEX: 101; LEFT: 25px; WIDTH: 206px; FONT-FAMILY: Verdana;

    POSITION: absolute" type="text" name="oText" size="29">
    <DIV style="DISPLAY: inline; Z-INDEX: 102; LEFT: 23px; WIDTH: 461px; POSITION: absolute; TOP:

    24px; HEIGHT: 25px" ms_positioning="FlowLayout">
    </DIV>
    <INPUT id="submit" style="Z-INDEX: 103; LEFT: 25px; WIDTH: 143px; POSITION: absolute; Top:

    50px" type="submit" value="Submit" name="submit">
    <DIV style="DISPLAY: inline; Z-INDEX: 104; LEFT: 28px; WIDTH: 268px; POSITION: absolute; TOP:

    78px; HEIGHT: 22px" ms_positioning="FlowLayout">
    </DIV>
    </form>
    </body>
    </HTML>

IMN logo majestic logo threadwatch logo seochat tools logo