VBA Internet Explorer is waiting for a web page to load

I know that such questions have been asked before, but mine are a bit different and were quite disturbing. I am dealing with a web page with a form with several events that load most of the page when some elements in the input field are filled. When these events reload the page again, but remain with the same url with the same prop name. I use the following types of methods, both separately and in combination with each other, to handle waiting for the page to load, but sometimes VBA still manages to continue execution and set the HTMLDocument variable to the page without the corresponding information about it, resulting in a macro for debugging. Here is what I am doing so far:

While IE.Busy DoEvents Wend Do Until IE.statusText = "Done" DoEvents Loop Do Until IE.readyState = 4 DoEvents Loop 

I even tried putting these events in a loop, as shown below, but it didn’t work because the lastModified property only returns the value to the second, and the macro rotates through the fields quickly enough, returning a new page in one 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 

Even this does not allow you to wait long enough to set the HTMLDocument object, leading to debugging. I assumed to install the next input element and verify that nothing contributes to further coding, but even this will not be successful for 100% of the time, because usually the input elements exist in HTML, but are hidden until the corresponding event is fired, which will not be a problem, but they do not load their possible choices until the event is fired. It could be a weird page.

In any case ... not sure what else to add. If there is anything else that might be helpful, just ask. I guess what I'm looking for is a way to make VBA wait until IE finds out that the other page is not on it. It seems to load several times before it is fully executed.

So ... Does anyone have any ideas?

EDIT: Found some new things to try. However, there are no dice. It has been suggested that I am adding these attempts. Here is the code, for some reason, the VBE and excel instance becomes immune when using this approach after triggering an event that should populate the parameters in the select element ... think of an xml attempt ... 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 

Based on what I see on the web page, I know that it is in this cycle that VBE and Excel become immune.

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

EDIT: Just thought I'd add this. When it comes to webpage automation, for the most part, I no longer use IE. I found this much better and completely circumvented this issue of asynchronous stuff by simply doing the recordings and getting it myself. It may not be the best solution, depending on what you are trying to do, but it works quite reliably if you carefully look at the traffic and evaluate the parameters well.

+7
dom internet-explorer vba automation
source share
3 answers

After an exhaustive search, I decided that the AJAX request, javascript code that runs asynchronously in the background, is not something I can get from any signal. It seems to trigger some kind of event when it ends up loading the page, and this is an option that I would like to explore in the future. However, for my purposes, I just used the same code that I already used to fill out the form on my page, and I again go through each field to check the values ​​are correct before clicking the submit button. This is not an ideal solution, but it is a workaround that seems to have taken care of my problem in this case.

I am not sure that my solution will be applicable to someone else dealing with this problem, but there if that helps. I think that workarounds for this problem should be based on the application in question and the web page.

+7
source share

I had the same issue with my page. What is done...

fist parameter

 While Ie.**document**.readystate="complete" DoEvents Wend 

there were several boxes in which the parameters were loaded after pressing the button / even a fire in another box ... I set jsst this way.

 Do Until IE.document.getelementbyid("Next box").Lenght>0 DoEvents Loop Application.wait Now+Timevalue("00:00:02) 
+1
source share

An old question that I know, but I think this is a good answer, which I have not seen much ...


I had a similar problem; waiting for all images to be loaded on the Google search engine (this is a tricky thing, as the image is requested by AJAX and the user scrolling the page) As suggested in the comments; my decision was to expect a certain element to appear in the viewport (this area is slightly larger than the monitor screen, and is considered as what you can actually see).

This is achieved using the getBoundingClientRect method getBoundingClientRect

 Dim myDiv As HTMLDivElement: Set myDiv = currPage.getElementById("fbar") 'this should be the last element on your page to load Dim elemRect As IHTMLRect: Set elemRect = myDiv.getBoundingClientRect Do Until elemRect.bottom > 0 'If the element is not in the viewport, then this returns 0 DoEvents 'Now run the code that triggers the Ajax requests 'For me that was simply scrolling down by a big number Set elemRect = myDiv.getBoundingClientRect Loop myDiv.ScrollIntoView 

I will explain in detail in a related answer how this works, but essentially BoundingClientRect.bottom is 0 until the element is in vieport. If this item is really the last download (for my Google search it was the Show More Results button), then while you get into the viewport when loading , you should be able to detect when it appears on the page. .bottom then returns a nonzero value (something in common with the actual position of the element on the page - I don't care, though for my purposes). I end up with .ScrollIntoView , but that is not essential.

0
source share

All Articles