2013-03-06 225 views
5

我在搜索框中輸入文本時遇到了一些麻煩,因爲我認爲這是相關ID標記。我從頁面的源代碼中獲得了ID。我之前在其他網站上做過這個。有人可以幫我嗎?有沒有另一種方法來做到這一點?VBA - IE GetElementByID不能正常工作

Sub FileUpload() 

Dim IEexp as Object 
IEexp.visible = True 
IEexp.Navigate ("www.example.com") 

'this is where the problem 
IEexp.Document.GetElementByID("step1_id_bean_newSupportingDoc_description").Value _ 
= "monthly update" 

End Sub 

我得到一個 「自動化錯誤調用該對象已經與其客戶端斷開」 我來自哪裏拉ID

源代碼:

<td class="Label">Description</td> 
    <td class="Data"><input type="text" name="bean.newSupportingDoc.description" size="60" maxlength="250" value="" id="step1_id_bean_newSupportingDoc_description" class="NoBorder"/> 
</td> 
+2

您是否正在等待文檔在嘗試訪問輸入元素之前加載? – 2013-03-06 21:12:52

+0

我在本地運行它,我等待頁面加載事件發生,我得到iwebbrowser2錯誤 – user1902540 2013-03-07 14:52:15

+0

是的,我已經運行在休息模式,所以我等待,直到頁面完全加載,我仍然得到該錯誤。 – 2013-03-07 15:22:03

回答

0

您可以嘗試

Do Until IEexp.readyState = 4 
DoEvents 
Loop 



IEexp.Document.getElementById("username").Value = "Monthly update" 


IEexp.Document.getElementById("password").Value = FilePth 
代碼起作用
0

'保護模式'怎麼樣?看到這篇文章:http://www.sevenforums.com/tutorials/63141-internet-explorer-protected-mode-turn-off.html。 如果您的IE瀏覽器以保護模式運行,請嘗試關閉它並再次運行代碼。

' Add References: 
' - Microsoft HTML Object Library 
' - Microsoft Internet Controls 

Sub FileUpload() 

    Dim IEexp As InternetExplorer 
    Set IEexp = New InternetExplorer 
    IEexp.Visible = True 
    IEexp.navigate "www.example.com" 

    Do While IEexp.readyState <> 4: DoEvents: Loop 

    Dim inputElement As HTMLInputElement 
    Set inputElement = IEexp.Document.getElementById("step1_id_bean_newSupportingDoc_description") 

    If (Not inputElement Is Nothing) Then 
     inputElement.Value = "monthly update" 
    Else 
     MsgBox "Input element not found on web page." 
    End If 

    IEexp.Quit 
    Set IEexp = Nothing 
End Sub 
1

如果您使用Set IEexp = New InternetExplorerMedium,則不必更改Internet選項中的設置。它使用中等完整性應用程序設置自動實例化IE對象。