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

查看:495
本文介绍了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

我甚至尝试这些事件放入像下面这样一个循环,但它确实不是那么回事,因为上次更改属性只返回一个值精确到秒,并且宏在田地里旋转速度不够快,这是在同一秒返回一个新的页面:

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的对象导致调试。我打算将下一个输入元素和检查,对于没有进一步的code,但因为一般输入元素在HTML中存在,但被隐藏,直至相应即使这样,也没有时间成功100%事件被触发,这不会是一个问题,但他们不加载其可能的选择,直到事件被触发后。这可能是一个奇数页。

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?

编辑:发现了一些新的东西去尝试。尽管如此,没有骰子。有人建议我加入这些尝试。这里是code,由于某些原因,VBE和烧制应该填充select元素中的选项......想尝试XML事件后,使用这种方法当Excel实例无响应...这里是code:

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浏览器。我发现这是好多了,完全回避的异步的东西这个问题,简单地执行职务,并得到自己。可能不取决于你想要做什么是最好的解决方案,但它工作可靠pretty如果您在交通仔细观察和参数事情做好。

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中code,它在后台异步运行是不是我可以得到一个以任何方式信号。它似乎当它与加载页面,这就是我想要的未来探索的一个选项完成触发某些事件。然而,我的目的,我只是用我已经使用填写表单我的网页上同一code和我有它遍历每个字段再次检查,看看是否该值仍然正确点击提交按钮前。它不是一个理想的解决方案,但它是出现在这种情况下采取了我的问题的关照一种解决方法。

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天全站免登陆