【问题标题】:Invalid Variant Operation error when access WebBrowser1.OleObject.Document.getElementById('Inputname').setAttribute访问 WebBrowser1.OleObject.Document.getElementById('Inputname').setAttribute 时出现无效的变体操作错误
【发布时间】:2016-11-09 13:32:20
【问题描述】:

我在 Delphi XE7 (win7, internet explorer 9) 中使用TWebBrowser 组件在网页中填写表格。

这是 HTML:

<input name="login" class="form-control" id="inputLogin" placeholder="Username" type="text">

我正在使用此代码:

WebBrowser1.OleObject.Document.getElementById('InputLogin').setAttribute('value','sometext');

它在我的电脑上运行良好,但在其他电脑上却给了我这个错误:

Invalid Variant Operation error.

我该如何解决这个问题?

【问题讨论】:

  • 我的猜测是另一台电脑缺少 dll。它可以是您自己的 Delphi 项目之一,也可以是浏览器组件所需的 MS dll。另一台 PC 是否有不同的浏览器版本? -- 另外,document.getElementById 区分大小写 --> 你的 ID 不是大写的 'Input....'

标签: delphi twebbrowser


【解决方案1】:

setAttribute 不是为input 元素设置/获取value 的首选方式。

使用IHTMLInputElement接口访问目标输入元素的value例如:

uses MSHTML;

var
  el: IHTMLElement;
  inputElement: IHTMLInputElement;

el := (WebBrowser1.Document as IHTMLDocument3).getElementById('inputLogin');
if Assigned(el) then
  if Supports(el, IID_IHTMLInputElement, inputElement) then
    inputElement.value := 'sometext';

我无法重现您遇到的错误,所以如果您坚持使用setAttribute,您可能需要尝试显式设置文档的接口,而不是访问OleObject.Document Variant。

例如:

el := (WebBrowser1.Document as IHTMLDocument3).getElementById('inputLogin');
if Assigned(el) then
  el.setAttribute('value', 'sometext', 0);

【讨论】:

  • 非常感谢 kobik,它非常有帮助,是的,确实使用 setattribut 导致了无效变体的问题(可能值为空)。再次感谢您,您为我节省了很多时间和精力。
  • 不客气。如果这回答了您的问题,请接受。顺便说一句,出于好奇:通过el.setAttribute 的第二种方法在另一台 PC 上对您有用吗?
  • 是的,我的第一个解决方案似乎变体的值始终为空
猜你喜欢
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
相关资源
最近更新 更多