VBA,doc.getElementsByClassName()在调试模式下而不在运行模式下工作 [英] VBA, doc.getElementsByClassName() works in debug mode not in run mode

查看:73
本文介绍了VBA,doc.getElementsByClassName()在调试模式下而不在运行模式下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在vba中使用以下代码,

I am using below code in vba,

Set results = Doc.getElementsByClassName("a-size-large a-color-price olpOfferPrice a-text-bold")

If results.Length > 0 Then

,但是长度仅在调试模式下给出> 1,而不是在运行模式下,我检查了站点并给出了上面提到的类,但是在调试模式下却无法逐行调试模式,并且无法在运行模式下正常工作

but the length only gives >1 in debug mode not in run mode, I checked the site and gives have having class above mentioned, but strangly works in debug mode that too line by line debug mode and does not work in run mode.

谢谢DJ

推荐答案

听起来好像您正在加载文档,但是在尝试检查文档之前还没有完成.在调试模式下运行时,文档有时间完成加载.强制VBA等待文档完全加载的最佳方法是在同步"模式下打开URL/文档.您还需要检查文档请求是否返回了有效的HTTP状态代码.

It sounds like you're loading the document, but it isn't complete before you try to inspect the document. When you run in debug mode, the document has time to complete loading. The best way to force VBA to wait for the document to load completely is to open the URL/Document in Synchronous mode. You'll also want to check that the document request returned a valid HTTP status code.

如果您的文档源是有效的XML/XHTML,则可以直接从XMLHttpRequest获取文档对象,但我假设您使用的是旧的HTML.

If your document source is valid XML/XHTML, then you can get a document object directly from the XMLHttpRequest, but I'm assuming you have plain old HTML.

您需要知道XMLHttpRequest将检索页面的HTML内容,但不会加载或运行任何支持脚本/css,因此如果这些脚本中的任何一个具有生成的动态HTML内容,那么它将不会出现在HTML文档中.

You need to be aware that the XMLHttpRequest will retrieve the HTML content of the page, but it will not load or run any of the supporting scripts/css, so if any of those scripts would have generated dynamic HTML content, then it won't be present in the HTML document.

Sub Test()

  Const URL As String = "http://stackoverflow.com"
  Const CLASS_NAME As String = "row-fluid"

  'The getElementsByClassName method is unavailable under some/all IE versions?
  'It seems to need to be strongly typed as MSHTML.HTMLDocument
  'You'll need to Add a reference to Microsoft HTML Object Libarary
  Dim oDoc As MSHTML.HTMLDocument

  Dim results As Object

  With CreateObject("MSXML2.XMLHttp")
    .Open "GET", "http://www.microsoft.com", False
    .send
    'Check the response is valid
    If .Status = 200 Then
      Set oDoc = CreateObject("htmlfile")
      oDoc.body.innerHTML = .responseText
      Set results = oDoc.getElementsByClassName(CLASS_NAME)
    End If
  End With

End Sub

这篇关于VBA,doc.getElementsByClassName()在调试模式下而不在运行模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆