5
votes

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.

2
WebBrowser1.OleObject.Document.getElementByID('register_vv[q][71]').Value:='test'; seems to work, no? - Sertac Akyuz
Nope, It doesn't work. I get an error message "Method 'value' not supported by automation object". - rookie Bounty
Weird, I copy pasted that line from a test app here (XE2-ie8-w7) - Sertac Akyuz
what version are you using? - Zeina
If you're question is directed at me, I'm using delphi 2009. - rookie Bounty

2 Answers

7
votes

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").

0
votes

Your getElementById('verification_control_0') will get the DIV Element, wich doesn't have a 'value'.

Your textbox does not have a ID. For a test change the 'name' attribute to 'id' like this

<input type="text" id="register_vv[q][71]" size="30" value=""  tabindex="6" class="input_text" />

and change your code like this

WebBrowser1.OleObject.Document.getElementById('register_vv[q][71]').value := 'mytext';