Excel VBA:在Internet Explorer中等待JavaScript执行

Excel VBA:在Internet Explorer中等待JavaScript执行,excel,internet-explorer,vba,automation,Excel,Internet Explorer,Vba,Automation,我正在尝试用Excel VBA做一些网页抓取。以下是我遇到问题的代码部分: IE.Navigate URL Do DoEvents Loop While IE.ReadyState <> 4 Or IE.Busy = True Set doc = IE.document 我可以通过执行Application.wait(Now+TimeValue(x))来等待执行,但这并不令人满意,因为脚本执行所需的时间取决于输入 有没有一种方法可以等待脚本完成评估,或者直接在doc对象中评估脚

我正在尝试用Excel VBA做一些网页抓取。以下是我遇到问题的代码部分:

IE.Navigate URL
Do
  DoEvents
Loop While IE.ReadyState <> 4 Or IE.Busy = True
Set doc = IE.document
我可以通过执行
Application.wait(Now+TimeValue(x))
来等待执行,但这并不令人满意,因为脚本执行所需的时间取决于输入


有没有一种方法可以等待脚本完成评估,或者直接在
doc
对象中评估脚本?

我找到了等待页面完成的代码。根据说明,它需要Microsoft Internet控件作为代码中的参考

代码在此复制,以防链接死亡:

'Following code goes into a sheet or thisworkbook class object module
Option Explicit

'Requires Microsoft Internet Controls Reference Library
Dim WithEvents ie As InternetExplorer
Sub start_here()
  Set ie = New InternetExplorer
  'Here I wanted to show the progress, so setting ie visible
  ie.Visible = True
  'First URL to go, next actions will be executed in
  'Webbrowser event sub procedure - DocumentComplete
  ie.Navigate "www.google.com"
End Sub
Private Sub ie_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  'pDisp is returned explorer object in this event
  'pDisp.Document is HTMLDocument control that you can use
  'Following is a choice to follow,
  'since there is no do-loop, we have to know where we are by using some reference
  'for example I do check the URL and do the actions according to visited URL

  'In this sample, we use google entry page, set search terms, click on search button
  'and navigate to first found URL
  'First condition; after search is made
  'Second condition; search just begins
  If InStr(1, URL, "www.google.com/search?") > 0 Then
    'Open the first returned page
    ie.Navigate pDisp.Document.getelementsbytagname("ol")(0).Children(0).getelementsbytagname("a")(0).href
  ElseIf InStr(1, URL, "www.google.com") > 0 Then
    pDisp.Document.getelementsbyname("q")(0).Value = "VB WebBrowser DocumentComplete Event"
    pDisp.Document.getelementsbyname("btnG")(0).Click
  End If
End Sub

实际上,您可以使用ie窗口评估javascript函数。但是你必须设置一个回调函数,因为函数的求值是异步的。

这篇文章很老了,但我也会回答这个问题,作为对我自己的回答,现在我已经知道了如何做

只需指向jQuery脚本运行后希望出现的内容,使用IE automation运行的JavaScript触发所需事件,并执行循环以等待所需内容出现

'This will trigger the jQuery event.
Doc.parentWindow.execScript "$('#optionbox').trigger('change')"

'This is the code that will make you wait. It's surprisingly efficient
Do While InStrB(Doc.getElementById("optionbox").innerHTML, "<desired html tag>") = 0
    DoEvents
Loop
”这将触发jQuery事件。
Doc.parentWindow.execScript“$('#optionbox')。触发器('change')”
'这是让您等待的代码。它的效率惊人
在InStrB(Doc.getElementById(“optionbox”).innerHTML“”)时执行此操作=0
多芬特
环

谢谢你的回答,肖恩。不幸的是,这不起作用,因为DocumentComplete事件在READYSTATE更改为READYSTATE_COMPLETE(=4)时触发,这正是我在解决方案中所做的。来源:此解决方案仅在浏览器的可见性设置设置为true时才有效,这在我的情况下是不可接受的。是否有脚本运行的某些结果可供检查?是否创建了页面元素,或者填写了一些表单值?如果没有“scriptfinishedrunning”事件,那么您必须检查脚本的输出或结果。如果不知道剧本的作用,很难说更多。谢谢你,蒂姆,这是一个好主意,尽管我觉得自己没有想到它有点愚蠢。我将检查SearchPage.Initialize是否仍在正文中。
'This will trigger the jQuery event.
Doc.parentWindow.execScript "$('#optionbox').trigger('change')"

'This is the code that will make you wait. It's surprisingly efficient
Do While InStrB(Doc.getElementById("optionbox").innerHTML, "<desired html tag>") = 0
    DoEvents
Loop