'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 1:[1]
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"
).
Solution 2:[2]
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';
Solution 3:[3]
I had to allow entry of text via touch buttons in to the active element, the below worked:
procedure TMyForm.SendKey(key : string);
var
Doc: IHTMLDocument2;
InputBox : IHtmlInputElement;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
if Doc.activeElement.isTextEdit then
begin
InputBox := Doc.activeElement as IHtmlInputElement;
InputBox.value := InputBox.value+key;
end;
end;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Matthias Alleweldt |
Solution 3 | user2814916 |