问题
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?
回答1:
One work-around could be to recognize when your script is stuck and then just send the keystroke "enter" to close the pop-up.
回答2:
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?
来源:https://stackoverflow.com/questions/9486847/close-javascript-alert-using-vba-automation