【发布时间】:2018-09-16 11:37:47
【问题描述】:
我有一个简单的代码,它使用 IE 自动化登录到网站(例如 URL1),然后单击链接(例如 URL2)并等待新页面准备好,依此类推。 代码如下:
'Part 1: Navigating to URL1
IE = CreateObject("InternetExplorer.Application")
IE.visible = True
IE.Navigate(URL1)
Do Until IE.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE
Application.DoEvents()
Loop
LinkSet1 = IE.document.all'Storing the current page's links only to help asking my question clearer :)
'Part 2: Entering user name and password and submit
IE.Document.All("UserNameElementID").InnerText = MyUserName
IE.Document.All("PasswordElementID").InnerText = MyPassword
IE.Document.All("SubmitElementID").click
Do Until IE.ReadyState = tagREADYSTATE.READYSTATE_COMPLETE
Application.DoEvents()
Loop
'Part 3: Search through links to detect a special id on the second page (URL2)
LinkFound = False
Do Until LinkFound
LinkSet2 = IE.document.all'Storing the new page's links only to help asking my question clearer :)
For Each Link In IE.document.all
If InStr(Link.id, "MYSecondURL_id") > 0 Then
LinkFound = True
Exit For
End If
Next
Loop
'Part 4: Send a message to show that the second URL is found
MsgBox("Page loaded completely!")
我的问题是,当我使用带有 IE 10 的 Windows 7 时,上面的代码运行良好。但是当我使用 IE 11 更新到 Windows 10 时,总是 LinkSet2 = LinkSet1 并且在第 3 部分发生无限循环。任何帮助将不胜感激提前!
【问题讨论】:
-
您应该使用WebBrowser Control 而不是
CreateObject("InternetExplorer.Application")。请注意,Use of Application.DoEvents() 可能会导致程序出现问题,请参阅How to wait until WebBrowser is completely loaded in VB.NET?,了解在文档加载完成后执行某些操作的正确方法。 -
谢谢安德鲁。老实说,我的代码在加载了几页后终于点击了一个链接来下载一个 Excel 文件。我首先出于我的目的使用了 WebBrowser Control,但是当单击下载链接时,它导致了“应用程序中的服务器”错误,经过大约 1 周的努力,我无法解决问题。因此,我改变了使用 IE 的方式。它完全可以在 Windows 7 和 IE 10 上运行,但不能在带有 IE 11 的 Windows 10 上运行。
标签: .net vb.net internet-explorer