VBA Internet Explorer 等待网页加载 [英] VBA Internet Explorer wait for web page to load

查看:105
本文介绍了VBA Internet Explorer 等待网页加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这样的问题,但我的问题有点不同,而且相当麻烦.我正在处理的是一个带有一些事件的表单的网页,当输入框中的某些项目被填写时,这些事件会加载更多的页面.当这些事件触发时,页面将再次加载,但仍保留在具有相同 nameprop 的相同 URL 中.我一直在单独使用以下类型的方法并串联在一起来处理等待页面加载,但有时 VBA 仍然设法继续执行并将 HTMLDocument 变量设置为页面,而页面上没有适当的信息导致宏调试.以下是我迄今为止一直在尝试的类型:

I know questions like this have been asked before, but mine is a bit different and has been fairly troubling. What I'm dealing with is a web page with a form with a few events that load more of the page when certain items in input boxes are filled out. When these events fire the page loads again, but remains at the same URL with the same nameprop. I've been using the following types of methods both seperately and strung together to handle waiting for the page to load, but sometimes the VBA still manages to continue executing and set the HTMLDocument variable to a page without the appropriate information on it causing the macro to debug. Here are the kinds of things I've been trying so far:

While IE.Busy
    DoEvents
Wend

Do Until IE.statusText = "Done"
    DoEvents
Loop

Do Until IE.readyState = 4
    DoEvents
Loop

我什至尝试将这些事件放入如下循环中,但它并没有完全奏效,因为 lastModified 属性只返回一个向下到第二个的值,并且宏在字段中旋转得足够快,以至于它是在同一秒内返回一个新页面:

I've even attempted to place these events into a loop like the following, but it didn't quite work because the lastModified property only returns a value down to the second and the macro spins through the fields fast enough that it is returning a new page in the same second:

Do Until IE.statusText = "Done" And IE.Busy = False And IE.ReadyState = 4 _
And IE.document.lastModified > LastModified ----or---- IE.document.nameprop = _
"some known and expected name prop here"
    While IE.Busy
        DoEvents
    Wend
    Do Until IE.statusText = "Done"
        DoEvents
    Loop
    Do Until IE.readyState = 4
        DoEvents
    Loop
Loop

即使无法等待足够长的时间来设置导致调试的 HTMLDocument 对象.我已经考虑设置下一个 input 元素并检查它以进一步推进代码,但即使这样也不会 100% 成功,因为通常 Input 元素存在于 HTML 中,但在触发适当的事件之前是隐藏的,这不会有问题,但在触发事件之前他们不会加载可能的选择.这可能是一个奇怪的页面.

Even that fails to wait long enough to set the HTMLDocument object leading to a debug. I've contemplated setting the next input element and checking that for nothing to further the code, but even that wouldn't be successful 100% of the time because generally the Input elements exist in the HTML but are hidden until the appropriate event is fired, which wouldn't be a problem but they don't load their possible selections until after the event is fired. It might be an odd page.

无论如何...不知道还有什么要补充的.如果还有其他可能有助于查看的内容,请询问.我想我正在寻找的是一种让 VBA 等待 IE 知道另一个页面不在路上的方法.好像要加载好几次才完全完成.

Anyway... not sure what else to add. If there is something else that might be helpful to see just ask. I guess what I'm looking for is a way to get VBA to wait until IE knows another page isn't on it's way. It seems to load a few times before it is completely done.

所以...有人有什么想法吗?

So... Anyone have any ideas?

找到了一些新的尝试.仍然没有骰子.有人建议我添加这些尝试.这是代码,由于某种原因,在触发应填充 select 元素上的选项的事件后使用此方法时,VBE 和 excel 实例变得无响应......考虑尝试 xml......这里是代码:

Found a few new things to try. Still, no dice. It was suggested that I add these attempts. Here is the code, for some reason the VBE and excel instance become non-responsive when using this approach after firing an event that should populate the options on the select element... thinking about trying xml... here is the code:

intCounter = 0
Do until intCounter > 2
    Do Until IE.Busy = False: DoEvents: Loop
    Do Until IE.ReadyState = 4: DoEvents: Loop
    Set HTMLDoc = IE.Document
    Do Until HTMLDoc.ReadyState = "complete"
    Set HTMLSelect = HTMLDoc.getElementById("ctl00$ctl00$MainContent$ChildMainContent$ddlEmployeeBranchCodes")
    intCounter = 0
    For each Opt in HTMLSelect
        intCounter = intCounter + 1
    Next Opt
Loop

根据我在网页上看到的情况,我知道在这个循环中的某个地方 VBE 和 Excel 变得无响应.

Based on what I can see happening on the web page, I know that it is somewhere in this loop that the VBE and Excel become non-responsive.

希望有帮助...我知道这对我没有帮助... Drats.

Hope that helps... I know it didn't help me... Drats.

只是想我会添加这个.当谈到自动化网页时,在大多数情况下,我不再使用 IE.我发现它要好得多,并且完全避开了异步问题的问题,只需执行帖子并获得自己.可能不是最好的解决方案,具体取决于您要做什么,但如果您仔细查看流量并很好地参数化,它会非常可靠地工作.

Just thought I'd add this. When it comes to automating a web page, for the most part, I no longer use IE. I've found it's much better, and sidesteps this issue of async stuff entirely, to simply perform the posts and gets yourself. May not be the best solution depending on what you're trying to do, but it works pretty reliably if you look at the traffic closely and parameterize things well.

推荐答案

经过详尽的搜索,我确定 AJAX 请求,在后台异步运行的 javascript 代码不是我可以从中获取信号的东西反正.当它完成加载页面时,它似乎确实会触发一些事件,这是我将来想探索的一个选项.但是,出于我的目的,我只是使用了我已经用来填写页面上的表单的相同代码,并让它再次循环遍历每个字段,以在单击提交按钮之前检查值是否仍然正确.这不是一个理想的解决方案,但它是一种解决方法,在这种情况下似乎已经解决了我的问题.

After an exhaustive search, I've determined that the AJAX request, javascript code that runs asynchronously in the background isn't something I can get a signal from in any way. It does seem to trigger some event when it finishes with loading the page, which is an option I'd like to explore in the future. However, for my purposes I simply used the same code I was already using to fill out the form on my page and I have it loop through each field again to check to see if the values are still correct before clicking the submit button. It isn't an ideal solution, but it is a workaround that appears to have taken care of my issue in this case.

我不确定我的解决方案是否适用于处理此问题的其他人,但如果有帮助,那就是了.我认为解决此问题的方法必须基于相关应用程序和网页.

I'm not sure if my solution would be applicable to someone else dealing with this issue, but there it is if it helps. I think workarounds for this issue are going to have to be based on the application and web page in question.

这篇关于VBA Internet Explorer 等待网页加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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