【问题标题】:IE does not update after clicking on a link点击链接后 IE 不更新
【发布时间】: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


【解决方案1】:

试试这个

Set window.onload = GetRef("WindowLoad")
Function WindowLoad
  Dim oElm 
Set oElm = document.getElementById("MYSecondURL_id")
if oElm Is Nothing then
    MsgBox("element does not exist")
else        
   MsgBox("element does exist")
end if
End Function

因为某些 VBscript 函数在较新版本的 IE 上不起作用。 毫无疑问,IE是上网史上最差的浏览器。

【讨论】:

  • 谢谢亲爱的纳迪尔。虽然这个答案没有解决我的问题,但我同意 IE 是最糟糕的浏览器!因此,我切换到 Mozilla 并使用 Geckofx 工具。这解决了我的问题。还是谢谢!
【解决方案2】:

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

'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

【讨论】:

  • 这个方法并没有解决我的问题,因为它总是进入“else”条件。正如我所解释的,LinkSet2 永远不会更新,它始终与 LinkSet1 相同(即第一页上的链接),尽管我在 IE 中看到第二页已完全加载..
  • 手动检查页面是否包含您正在搜索的 ID 内容。
  • 不不,我知道第二页肯定包含我想要的链接;该循环只是为了确保新页面已加载(因为我想单击该链接以转到第三页!)。 IE.ReadyState 循环是不够的,因为虽然它说页面加载完成但不是:))
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 2015-09-06
  • 2013-04-20
  • 2014-01-30
  • 1970-01-01
  • 2011-06-20
  • 2011-04-04
  • 2017-03-19
相关资源
最近更新 更多