在VBA脚本中单击Javascript确认button
我的代码的目的是在具有指定标准的网站上运行报告。 我想search所有的“距离”标准,这会提示一个消息框询问我是否确定。 我希望我的代码能够点击“确定”button。 但是,一旦“提交”button被击中并且被挂起,直到“确定”button被击中,我的代码就会停止运行。
注意:我无法更改HTML或JavaScript
这是VBA代码
Sub Data_Grab() 'Open Internet Explorer and webpage Dim IE As Object Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.Navigate "insert website here" 'wait for page to finish loading Do While IE.Busy Loop 'get a reference to the search form by finding the id in the web page's object model Dim daysAs Object Dim city As Object Set days= IE.Document.getElementByID("age"): days.Value = "10" Set city= IE.Document.getElementByID("location"): city.Value = "LA" 'click the search button With IE.Document Set elems = .getElementsByTagName("input") For Each e In elems If (e.getAttribute("value") = "Submit") Then e.Click Exit For End If Next e End With End Sub
来自网站源的相关HTML / JavaScript代码
if (!distanceSelect) { proceed = confirm("Are you sure you wish to search all " + question + "? Performance issues may occur."); } else {proceed = true;}
和
<input class="contentarea" type="button" value="Submit" id="submit" onclick="javascript:loadReport();" />
我已经尝试了许多不同的解决scheme,如SendKeys和创build一个ShellScript,但我一直没能得到任何工作。
谢谢。
一种方法是用execScript
重写confirm
:
Dim ie As Object, win As Object Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.Navigate "https://stackoverflow.com" Do While ie.Busy: DoEvents: Loop Set win = ie.Document.parentWindow win.execScript "window.confirm = function(){return true;};"