delphitwebbrowser

Delphi Twebbrowser change value of textbox.


I'm have some difficulty changing the value of a textbox in twebbrowser. I have tried WebBrowser1.OleObject.Document.getElementById('verification_control_0').value := 'mytext'; and a few other methods but it doesn't seem to work.

The websites code:

 <div id="verification_control_0" class="verification_control">
 <div class="smalltext">
 What are the first 3 letters of our website's name?:<br />
 <input type="text" name="register_vv[q][71]" size="30" value=""  tabindex="6"    class="input_text" />
  </div>
 </div>

If you could please show me how to change the value in <input type="text" name="register_vv[q][71]" size="30" value="" tabindex="6" class="input_text" /> i would really appreciate it very much. Thanks for reading and all replies.


Solution

  • Try this:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      col: IHTMLElementCollection;
      el: IHTMLInputElement;
    begin
      col := (WebBrowser1.Document as IHTMLDocument3).getElementsByName('register_vv[q][71]');
      if col.length <> 0 then
      begin
        el := col.item(0, 0) as IHTMLInputElement;
        el.value := 'mytext';
      end;
    end;
    

    In IE8 Standards mode, getElementById performs a case-sensitive match on the ID attribute only.
    In IE7 Standards mode and previous modes, this method performs a case-insensitive match on both the ID and NAME attributes, which might produce unexpected results.

    So if your TWebBrowser works with IE7 Standards mode and previous modes getElementById should work as well:

    procedure TForm1.Button2Click(Sender: TObject);
    var
      el: IHTMLElement;
      inputElement: IHTMLInputElement;
    begin
      el := (WebBrowser1.Document as IHTMLDocument3).getElementById('register_vv[q][71]');
      if Assigned(el) then
        if Supports(el, IID_IHTMLInputElement, inputElement) then
          inputElement.value := 'mytext';
    end;
    

    Using getElementsByName collection to locate elements by NAME should be the preferred solution.


    EDIT: @SertacAkyuz first comment:

    WebBrowser1.OleObject.Document.getElementByID('register_vv[q][71]').Value:='tes‌​t';
    

    I'm pretty sure OP did not test your code (which should work by default, unless OP explicitly changed IE browsing mode), and used getElementByID('verification_control_0') instead - which is a DIV element and does not have method value supported. (hence the error message "Method 'value' not supported by automation object").