VBA - IE GetElementByID not working

为君一笑 提交于 2019-12-05 23:57:42

问题


I'm having some trouble with entering a text in a search box when after I what I think to be the correcet ID tag is. I got the ID from the page's source code. I've done this before with other websites. Can someone please help me out? Is there another way to do this?

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

I get a "Automation Error The Object invoked has disconnected from its clients"

Source Code where I pulled the ID from:

<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>

回答1:


If you use Set IEexp = New InternetExplorerMedium you don't have to change the settings in your Internet Options. It automatically instantiates the IE object with Medium Integrity Application settings.




回答2:


You can try

Do Until IEexp.readyState = 4
DoEvents
Loop



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


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



回答3:


The code works. What about 'protection mode'? See this article: http://www.sevenforums.com/tutorials/63141-internet-explorer-protected-mode-turn-off.html. If your IE browser runs in protection mode then try to turn it off and run the code again.

' 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


来源:https://stackoverflow.com/questions/15257179/vba-ie-getelementbyid-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!