2013-03-06 16 views
5

Ho qualche problema con l'immissione di un testo in una casella di ricerca quando dopo quello che penso sia il tag ID correcet. Ho ricevuto l'ID dal codice sorgente della pagina. L'ho già fatto con altri siti web. Qualcuno può aiutarmi? c'è un altro modo per fare ciò?VBA - IE GetElementByID non funziona

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 

ottengo un "Errore di automazione L'oggetto invocato si è disconnesso dai client"

codice sorgente in cui ho tirato l'ID da:

<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

Stai aspettando che il documento venga caricato prima di tentare di accedere all'elemento di input? –

+0

l'ho eseguito localmente e ho aspettato che si verificasse l'evento di caricamento della pagina e ottengo l'errore iwebbrowser2 – user1902540

+0

Sì, l'ho eseguito in modalità interruzione, quindi aspetto che la pagina venga caricata completamente e ottengo ancora quell'errore. –

risposta

0

si può provare

Do Until IEexp.readyState = 4 
DoEvents 
Loop 



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


IEexp.Document.getElementById("password").Value = FilePth 
0

Il codice funziona. Che dire di "modalità di protezione"? Vedi questo articolo: http://www.sevenforums.com/tutorials/63141-internet-explorer-protected-mode-turn-off.html. Se il browser IE viene eseguito in modalità di protezione, provare a disattivarlo e rieseguire il codice.

' 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

Se si utilizza Set IEexp = New InternetExplorerMedium non c'è bisogno di cambiare le impostazioni in Opzioni Internet. Crea automaticamente un'istanza dell'oggetto IE con le impostazioni di Media Integrity Application.