首页 > 解决方案 > 点击链接后 IE 不更新

问题描述

我有一个简单的代码,它使用 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 部分发生无限循环。任何帮助都将提前感谢!

标签: .netvb.netinternet-explorer

解决方案


这是因为您使用的是直到找到链接。这意味着如果没有链接,这个循环将永远不会结束。反而。我建议您仅用于每个循环。并在循环结束时(当循环退出时。)检查是否找到链接。并执行相应的操作。

'Part 3: Search through links to detect a special id on the second page (URL2)
LinkFound = False 
 LinkSet2 = IE.document.all     
 For Each Link In IE.document.all
     If InStr(Link.id, "MYSecondURL_id") > 0 Then 
         LinkFound = True               
         Exit For
     End If
 Next
If LinkFound=True
'Do what you want to do if there is link
else
 'Do when there is no link in the page
End If

推荐阅读