Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am searching for a way to close a Javascript pop-up/message box (i.e. not a NEW IE window, but a scripted alert()) that loads when a webpage is loaded.

I currently have code that loads data into a search form, runs the search, retrieves the needed information from the results, exits the search, then continues with the next item in a list. In some cases, there is a piece of critical information missing from the search results. When this happens, the JavaScript on the page pops up the message when the page loads, letting the user know that info is missing. With the automation the code halts(waits) for the box to clear before continuing, and the user must click on the OK button before this will happen.

(I have no way to change the source script of the page directly, as that is outside of my scope of responsibility here at work. Plus, it is more important to retain this functionality for general use; it would really only benefit this small-usage function I'm creating.)

While I can't change the source script, I have found an example of how to prevent a pop-up/alert from displaying by changing the loaded script. However, it relates to an already-loaded page and doesn't really work for me (as far as I can tell).

IE.Document.getElementById("clearRelItems").removeAttribute "onClick"
IE.Document.getElementById("clearRelItems").setAttribute "onClick", "return true;"

Is it possible to use this method to make a change before the page has fully loaded? i.e. can this be used to somehow bypass/circumvent the function call at page load?

I know about sendkeys, but I would prefer to avoid this option if at all possible (I may end up using this option if no other alternative exists). This function is intended to be initiated by the user, then left running in the background as it will take some time to complete.

I have looked into grabbing the XML as suggested by @Kyle, but I don't believe I am proficient enough to make this method work.

How else might I get around the alert? Is there a way to actually activate the OK button on the alert? Can the alert/loading of the page be bypassed any other way?

share|improve this question
    
Is this a Javascript alert(); message box? Or a html element? –  Cilvic Feb 28 '12 at 20:47
    
This is an alert() box. –  Gaffi Feb 28 '12 at 20:48

2 Answers 2

up vote 2 down vote accepted

One work-around could be to recognize when your script is stuck and then just send the keystroke "enter" to close the pop-up.

share|improve this answer
    
I know about sendkeys, but this is intended to be initiated by the user, then left running in the background as it will take some time to complete. I may end up using this option if no other alternative exists. (adding this info to the original quesiton) –  Gaffi Feb 28 '12 at 20:51
    
I still would rather not, but I'm going to have to go with this option. Thanks! –  Gaffi Mar 2 '12 at 1:18
    
SendKeys was not able to successfully close a JavaScript confirm() popup in my case. Adding Application.Wait before SendKeys also didn't work. Anyone else have this issue? –  10basetom Oct 23 '12 at 10:26
    
Never mind, I found out it wasn't working because I was adding True for the Wait parameter. Make sure to leave the second parameter empty when you use SendKeys to close the alert box. –  10basetom Oct 23 '12 at 10:36

Have you had a look at using the MS XML classes for posting the data directly and parsing the result? This is much faster than IE automation although admittedly not always possible

Edit: Not really, we're just using the classes to handle the request and response, we don't actually use XML, I put this together for someone a while back to give you an idea:

Sub GetDataXML()

Dim strPostText As String, strResponse As String
Dim Pressure As Double, Temperature As Double
Dim XMLrequest As Object

Pressure = CDbl(InputBox("Enter the Pressure"))
Temperature = CDbl(InputBox("Enter the Temperature"))

strPostText = "lang=english&calc=standard&druck=" & Pressure & "&druckunit=1&temperatur=" & Temperature & "&tempunit=1&Submit=Calculate"

Set XMLrequest = CreateObject("MSXML2.XMLHTTP")

With XMLrequest
    .Open "POST", "http://www.peacesoftware.de/einigewerte/calc_co2.php5", False
    .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    .send (strPostText)
    Do: DoEvents: Loop Until .readyState = 4
    strResponse = .responseText
    .abort
End With

Debug.Print strResponse

End Sub

It is even easier if you are using a query table in excel as this handles any parsing of the data.

Does the above help you?

share|improve this answer
    
I have successfully gotten your example to work, @Kyle. However, there is something else going into the POST command that I am missing (this is assumption based on my utter lack of knowledge here). I cannot identify what variables are passed, nor can I tinker with my unknowns, as a script directs me to a login page each time I do this outside the scope of the original page. The Debug.Print outputs the code for the standard Login screen I am accustomed to. How am I able to view these variables? Is there one for each INPUT/OPTION tag? (If so, there are quite a few to work with.) –  Gaffi Mar 1 '12 at 19:46
    
This approach isn't always possible. The easiest way to get your POST variables is from your browser in development tools, which browser are you using? –  Kyle Mar 2 '12 at 9:08
    
This is for work, and my only option is IE 7. –  Gaffi Mar 2 '12 at 11:08
    
Would you be able to install something like: this web analyser –  Kyle Mar 2 '12 at 12:54
1  
ok and I'm guessing that it's an internal site that you are trying to access? - if it's external I can have a look, if not then I'm afraid you may well be stuck with the send keys approach :( –  Kyle Mar 2 '12 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.