.net - 点击链接后 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 部分发生无限循环。任何帮助都将提前感谢!
解决方案
这是因为您使用的是直到找到链接。这意味着如果没有链接,这个循环将永远不会结束。反而。我建议您仅用于每个循环。并在循环结束时(当循环退出时。)检查是否找到链接。并执行相应的操作。
'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
推荐阅读
- c# - 如何停止处于等待用户请求状态的线程
- node.js - 将 json 值转换为 var
- wcf - WCF抛出服务器拒绝客户端凭据的异常,WCF中NetTCP的默认安全模式是什么
- android - Android - 导入 AAR 时出错,错误:找不到属性
- android - 无法解决资源链接失败错误
- python - 使用列表存储折扣代码,并在兑换后将其从列表中删除
- angular - 如何在 Angular 材质中制作悬挂效果?
- mysql - mysql:使用多个唯一索引插入忽略
- asp.net-core-mvc - 如何克服最终一致性?
- c# - c# int 和 string 相同的值但带有两个不同的数字