vb.net VB.net如何正确控制IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21567942/
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
VB.net how to control IE the right way
提问by Maarten
I use this for loading a website in IE:
我用它在 IE 中加载网站:
Dim ie As Object
ie = CreateObject("InternetExplorer.Application")
ie.Navigate("http://xxxxxxx.com/login.html")
ie.Visible = True
And this for filling input boxes right after i load the page:
这用于在我加载页面后立即填充输入框:
ie.Document.GetElementById("email").SetAttribute("value", "[email protected]")
The problem comes when i want to click a button with the use of my VB app. I use this code in a button:
当我想使用我的 VB 应用程序单击一个按钮时,问题就出现了。我在按钮中使用此代码:
Dim ie As Object
ie = CreateObject("InternetExplorer.Application")
ie.goback()
Example is to use the back button from IE but even when i use ie.Document.All("Login").InvokeMember("click")i get a (Exception from HRESULT: 0x80004005 (E_FAIL)) error.
示例是使用 IE 的后退按钮,但即使我使用时,我也会ie.Document.All("Login").InvokeMember("click")收到(来自 HRESULT 的异常:0x80004005 (E_FAIL))错误。
Do i need to tell my commands what tab/window it needs to use or something else i'm missing? I want to do stuff on websites opened with the standard Internet Explorer, i'm using version 11 at the moment (maybe that is a problem ;-))
我是否需要告诉我的命令它需要使用哪个选项卡/窗口或我缺少的其他东西?我想在使用标准 Internet Explorer 打开的网站上做一些事情,我目前使用的是 11 版(也许这是一个问题;-))
edit: this is how i solved clicking the login button but i need to click other links by anchor text :(
编辑:这就是我解决单击登录按钮的方法,但我需要通过锚文本单击其他链接:(
ie.Document.GetElementById("pass").Focus()
SendKeys.Send("{ENTER}")
回答by Josh Anstead
The method that you are using will work for IE8 and below. In order to use it with IE9 through IE11, you need to add the following code. This will invoke the event listeners.
您使用的方法适用于 IE8 及以下版本。为了在 IE9 到 IE11 中使用它,您需要添加以下代码。这将调用事件侦听器。
Reference Microsoft HTML Object Library.
参考 Microsoft HTML 对象库。
Dim Click_it As Object
Click_it = ie.document.createEvent("HTMLEvents")
Click_it.initEvent "click", True, False
ie.document.Element_to_Click.dispatchEvent Click_it

