VBA - IE GetElementByID 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15257179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA - IE GetElementByID not working
提问by Andy Caster
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?
在我认为正确的 ID 标签之后,我在搜索框中输入文本时遇到了一些麻烦。我从页面的源代码中获得了 ID。我以前在其他网站上做过这个。有人可以帮我吗?有没有另一种方法可以做到这一点?
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:
我从中提取 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>
回答by cyphi1
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.
如果您使用Set IEexp = New InternetExplorerMedium
,则不必更改 Internet 选项中的设置。它使用中等完整性应用程序设置自动实例化 IE 对象。
回答by user1902540
You can try
你可以试试
Do Until IEexp.readyState = 4
DoEvents
Loop
IEexp.Document.getElementById("username").Value = "Monthly update"
IEexp.Document.getElementById("password").Value = FilePth
回答by dee
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.
该代码有效。“保护模式”呢?请参阅这篇文章: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