使用 VBA 自动化关闭 JavaScript 警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9486847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Close JavaScript Alert Using VBA Automation
提问by Gaffi
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.
我正在寻找一种方法来关闭加载网页时加载的 Javascript 弹出窗口/消息框(即不是新的 IE 窗口,而是脚本化的alert())。
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.
我目前有代码将数据加载到搜索表单中,运行搜索,从结果中检索所需的信息,退出搜索,然后继续列表中的下一项。在某些情况下,搜索结果中缺少一条关键信息。发生这种情况时,页面上的 JavaScript 会在页面加载时弹出消息,让用户知道信息丢失。通过自动化,代码在继续之前暂停(等待)框被清除,并且用户必须在这发生之前单击“确定”按钮。
(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 exampleof 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 beforethe 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.
我知道 sendkeys,但如果可能的话,我宁愿避免使用此选项(如果不存在其他替代方法,我可能最终会使用此选项)。此功能旨在由用户启动,然后在后台运行,因为它需要一些时间才能完成。
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.
我已经按照@Kyle 的建议研究了获取 XML,但我不相信我的熟练程度足以使这种方法起作用。
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?
我还能如何绕过警报?有没有办法实际激活警报上的确定按钮?可以通过任何其他方式绕过页面的警报/加载吗?
采纳答案by Cilvic
One work-around could be to recognize when your script is stuck and then just send the keystroke "enter" to close the pop-up.
一种解决方法可能是识别您的脚本何时卡住,然后只需发送击键“输入”即可关闭弹出窗口。
回答by SWa
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
您是否查看过使用 MS XML 类直接发布数据并解析结果?这比 IE 自动化要快得多,尽管不可否认并非总是可行
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:
编辑:不是真的,我们只是使用类来处理请求和响应,我们实际上并不使用 XML,我把它放在一起给某人一段时间来给你一个想法:
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.
如果您在 excel 中使用查询表,则更容易,因为它可以处理任何数据解析。
Does the above help you?
以上对你有帮助吗?