访问 WebBrowser1.OleObject.Document.getElementById('Inputname').setAttribute 时出现无效的变体操作错误
Invalid Variant Operation error when access WebBrowser1.OleObject.Document.getElementById('Inputname').setAttribute
我正在使用 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');
它在我的 PC 上运行良好,但在其他 PC 上却出现以下错误:
Invalid Variant Operation error.
我该如何解决这个问题?
对于 input
元素,setAttribute
不是 set/get 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
变体。
例如:
el := (WebBrowser1.Document as IHTMLDocument3).getElementById('inputLogin');
if Assigned(el) then
el.setAttribute('value', 'sometext', 0);
我正在使用 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');
它在我的 PC 上运行良好,但在其他 PC 上却出现以下错误:
Invalid Variant Operation error.
我该如何解决这个问题?
input
元素,setAttribute
不是 set/get 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
变体。
例如:
el := (WebBrowser1.Document as IHTMLDocument3).getElementById('inputLogin');
if Assigned(el) then
el.setAttribute('value', 'sometext', 0);