Search code examples
delphiinputselected

Detect Twebbrowser inputbox click in delphi XE2


I am looking at creating autoform filler for Delphi, and obviously need a good method to capture which input boxes are the login boxes on each site so was wondering if I use a Twebbrowser component and load the page then click on the username and password boxes on the particular sites if I can then extract the form name and input box names that I clicked on.

In Short I need delphi to capture the name of the selected input box on a web page loaded into a twebbrowser component.

Any good methods to capture this information from the page loaded in a twebbrowser page would be appreciated!.


Solution

  • The following code shows how to find an INPUT element named 'input1':

    var
      E : IHtmlElement;
      D : IHtmlDomNode;
      Doc2 : IHtmlDocument2;
      Doc3 : IHtmlDocument3;
      All : IHTMLElementCollection;
      i : Integer;
    
    begin
      Doc3 := WebBrowser1.Document as IHtmlDocument3;
      D := Doc3.GetElementByID('input1') as IHtmlDomNode;
      if D <> Nil then begin
        ...
    

    If you need to find more than one INPUT element or you wish to pattern-match the name of the INPUT element(s), you can do this by retrieving the document's IHtmlDocument2 interface and then iterating its all collection:

      Doc2 := WebBrowser1.Document as IHtmlDocument2;
      All := Doc2.all;
      for i := 0 to All.Length - 1 do begin
        E := All.Item(Null, i) as IHtmlElement;
        // Test E and do what you like with it
      end;
    

    You could use a function like this to find the parent FORM element of an INPUT element

    function GetParentFormElement(E : IHtmlElement) : IHtmlElement;
    begin
      Result := Nil;
      while E <> Nil do begin
        if CompareText(E.tagName, 'form') = 0 then begin
          Result := E;
          exit;
        end;
        E := E.parentElement;
      end;
    end;
    

    and use it like this:

    E := D as IHtmlElement;
    E := GetParentFormElement(E);
    Assert(E <> Nil);
    

    not all forms have a name or id so how do I get the number or reference of a parent form if there are a number of forms in a page?

    Equally, not all INPUT elements are contained in a FORM one. TBH, I don't know of a robust way of doing what you want which will survive a page's author making changes to it. Anyway, there must be some way of identifying a given INPUT element, otherwise the server wouldn't be able to extract the user's response, mustn't there? So it's just a question of figuring out what that might be for a particular page. It it is not in the attributes of the element, then maybe you could look for the text of a nearby text element - after all, there must be some kind of prompt to the user to tell them what to fill in where. But this is really a different issue than the substance of your original q, which I hope I have answered. If you need more help on this specific point, I suggest you ask in a new q. Make sure you include details (code) of what you've already tried, as qs lacking them tend not to be received well at SO.