Delphi Twebbrowser更改文本框的值.

roo*_*nty 5 delphi twebbrowser

我在twebbrowser中更改文本框的值有些困难.我尝试WebBrowser1.OleObject.Document.getElementById('verification_control_0').value := 'mytext';了一些其他方法,但它似乎没有用.

网站代码:

 <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>
Run Code Online (Sandbox Code Playgroud)

如果你能告诉我如何改变价值,<input type="text" name="register_vv[q][71]" size="30" value="" tabindex="6" class="input_text" />我会非常感激.感谢阅读和所有回复.

kob*_*bik 7

试试这个:

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;
Run Code Online (Sandbox Code Playgroud)

在IE8标准模式下,仅对属性getElementById执行区分大小写的匹配ID.
在IE7标准模式和以前的模式中,此方法对IDNAME属性执行不区分大小写的匹配,这可能会产生意外结果.

因此,如果您TWebBrowser使用IE7标准模式和以前的模式getElementById也可以正常工作:

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;
Run Code Online (Sandbox Code Playgroud)

使用getElementsByName集合来定位元素NAME应该是首选解决方案.


编辑: @SertacAkyuz第一条评论:

WebBrowser1.OleObject.Document.getElementByID('register_vv[q][71]').Value:='tes??t';
Run Code Online (Sandbox Code Playgroud)

我很确定OP没有测试你的代码(默认情况下应该可以工作,除非OP明确改变IE浏览模式),getElementByID('verification_control_0')而是使用- 这是一个DIV元素并且没有value支持方法.(因此错误信息"Method 'value' not supported by automation object").