单击VBA脚本中的Javascript确认按钮 [英] Clicking Javascript confirmation button in VBA script
问题描述
我的代码的目的是在具有指定条件的网站上运行报告.我想搜索所有的距离"条件,这会提示一个消息框,询问我是否确定.我希望我的代码能够单击确定"按钮.但是,一旦单击提交"按钮,我的代码就会停止运行,并且会一直挂起,直到单击确定"按钮.
The purpose of my code is to run a report on a website with specified criteria. I want to search for all of the "distance" criteria, which prompts a message box asking if I'm sure. I want my code to be able to click the "Ok" button. However, my code stops running once the "Submit" button is hit and is hung until the "Ok" button is hit.
注意:我无法更改HTML或JavaScript
Note: I cannot change the HTML or 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代码
The relevant HTML/JavaScript code from the website source
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,但是我无法使它们中的任何一个起作用.
I've tried numerous different solution, such as SendKeys and creating a ShellScript, but I haven't been able to get any of them to work.
谢谢.
推荐答案
一种方法是使用 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;};"
这篇关于单击VBA脚本中的Javascript确认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!