我的代码的目的是在具有指定条件的网站上运行报告。我想搜索所有“距离”标准,这会提示一个消息框询问我是否确定。我希望我的代码能够单击“确定”按钮。但是,一旦点击“提交”按钮,我的代码就会停止运行并挂起,直到命中“确定”按钮为止。
注意:我无法更改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();" />
我尝试了很多不同的解决方案,例如SendKeys和创建ShellScript,但我无法让它们中的任何一个工作。
谢谢。
答案 0 :(得分:1)
一种方法是使用confirm
覆盖execScript
:
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;};"