VBA - IE GetElementByID not working

2019-04-07 07:25发布

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>

3条回答
来,给爷笑一个
2楼-- · 2019-04-07 08:06

You can try

Do Until IEexp.readyState = 4
DoEvents
Loop



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


IEexp.Document.getElementById("password").Value = FilePth
查看更多
神经病院院长
3楼-- · 2019-04-07 08:16

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
查看更多
4楼-- · 2019-04-07 08:17

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.

查看更多
登录 后发表回答