2016-11-09 153 views
1

我在Delphi XE7(win7,Internet Explorer 9)中使用TWebBrowser組件來填充網頁中的表單。當訪問WebBrowser1.OleObject.Document.getElementById('Inputname')時出現無效的Variant操作錯誤。setAttribute

下面是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.

我該如何解決這個問題?

+1

我的猜測是,其他PC缺少DLL。它可以是你自己的Delphi項目或瀏覽器組件所需的MS DLL。另一臺PC是否有不同的瀏覽器版本? - 另外,document.getElementById是區分大小寫的 - >您的ID不是大寫字母'Input ....' – Thor

回答

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變體。

例如爲:

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

非常感謝kobik,它非常有幫助,確實使用setattribut導致無效變體問題可能該值爲空)。再次感謝你,你爲我節省了很多時間和精力。 – kaleeeed

+1

不客氣。如果這回答你的問題,請接受它。順便說一句,出於好奇:第二種方法通過'el.setAttribute'在另一臺PC上爲你工作嗎? – kobik

+0

是的,它似乎是我的第一個解決方案變體的值始終爲空 – kaleeeed

相關問題