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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13029708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:11:11  来源:igfitidea点击:

Excel VBA: Wait for JavaScript execution in Internet Explorer

excelinternet-explorervbaautomation

提问by Jonas Raedle

I am trying to do some web scraping in Excel VBA. Here is the part of the code that I am having trouble with:

我正在尝试在 Excel VBA 中进行一些网页抓取。这是我遇到问题的代码部分:

IE.Navigate URL
Do
  DoEvents
Loop While IE.ReadyState <> 4 Or IE.Busy = True
Set doc = IE.document

After running this doccontains html that still has unexecuted JavasScript in it. This is the signature of the script that has not been executed:

运行后,它doc包含的 html 中仍然有未执行的 JavasScript。这是尚未执行的脚本的签名:

<SCRIPT type=text/javascript>
        goosSearchPage.Initialize(...)...;
</SCRIPT>

I can wait for execution by doing Application.Wait(Now + TimeValue(x))but that really is not satisfactory, as the amount of time the script takes to execute is quite variable depending on input.

我可以通过这样做来等待执行,Application.Wait(Now + TimeValue(x))但这确实不令人满意,因为脚本执行所需的时间根据输入而变化很大。

Is there a way to either wait for the script to finish evaluating or to just evaluate the script directly in the docobject?

有没有办法等待脚本完成评估或直接在doc对象中评估脚本?

回答by SeanC

I found code that does wait for a page to complete. per the notes here, it requires the Microsoft Internet Controlsas a reference in your code.

我找到了等待页面完成的代码。根据此处的注释,它需要Microsoft Internet Controls作为您代码中的参考。

Code reproduced here, just in case the link dies:

此处转载代码,以防链接失效:

'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

回答by Iwan1993

You actually can evaluate the javascript function with the ie window. But you gotta set up a Callback because the function will be evaluated async.

您实际上可以使用 ie 窗口评估 javascript 函数。但是您必须设置回调,因为该函数将被异步评估。