vba 使用 IE 浏览时处理弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17773523/
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
Handle Pop-Up While Navigating with IE
提问by ron
I have an Excel VBA macro that
我有一个 Excel VBA 宏
- opens IE,
- navigates to the Medicare website,
- logs me in,
- compares the claims listed on the website with those already in the workbook
- alerts me to any differences
- 打开 IE,
- 导航到 Medicare 网站,
- 让我登录,
- 将网站上列出的声明与工作簿中已有的声明进行比较
- 提醒我任何差异
It is during the log-in step that I have a problem, so I've reproduced that portion of the code below. As soon as the .click
line is executed a pop-up window appears asking the user to click the OKbutton in order to proceed. Macro execution is suspended until I manually click the OKbutton on the pop-up.
正是在登录步骤期间,我遇到了问题,所以我复制了下面的那部分代码。一旦.click
执行该行,就会出现一个弹出窗口,要求用户单击该OK按钮以继续。宏执行暂停,直到我手动单击OK弹出窗口上的按钮。
The source code behind the http://www.mymedicare.govweb page has information relative to the pop-up, but I haven't been able to figure out how to use it so that I can programmatically click the pop-up OKbutton.
http://www.mymedicare.gov网页背后的源代码有与弹出窗口相关的信息,但我一直无法弄清楚如何使用它,以便我可以以编程方式单击弹出OK按钮.
Any help in terms of figuring out how to programmatically click the pop-up's OKbutton would be much appreciated.
在弄清楚如何以编程方式单击弹出窗口OK按钮方面的任何帮助将不胜感激。
note: For the purpose of this question, any user id and password can be used. If you handle the pop-up you'll be passed to a page that says something like incorrect user id / password.
That will indicate that you've been successful in handling the pop-up.
注意:对于此问题,可以使用任何用户 ID 和密码。如果您处理弹出窗口,您将被传递到一个页面,上面写着不正确的用户 ID/密码之类的内容。
这将表明您已成功处理弹出窗口。
Sub Medicare_Claims()'
' Update the status bar
Application.StatusBar = "Running the Medicare Claims subroutine"
' Open the "MyMedicare web page
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "https://www.mymedicare.gov/"
End With
' Loop until the page is fully loaded
Do Until ie.ReadyState = 4 And Not ie.Busy
DoEvents
Loop
' Log-in
ie.Document.all.Item("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEUserName").Value = "abcde"
ie.Document.all.Item("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEPassword").Value = "12345"
ie.Document.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SignIn").Click
' Loop until the page is fully loaded
Do Until ie.ReadyState = 4 And Not ie.Busy
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:15"))
' Navigate further to the Search Claims" web page
ie.Navigate "https://www.mymedicare.gov/searchclaims.aspx"
采纳答案by ron
In case anyone passes this way again, here is the solution provided by Tim Williams:
如果有人再次通过这种方式,这是蒂姆威廉姆斯提供的解决方案:
'Log-in
ie.Document.all.Item"ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEUserName").Value= "abcde"
ie.Document.all.Item("ctl00_ContentPlaceHolder1_ctl00_HomePage_SWEPassword").Value = "12345"
Dim f As String
f = "function(SignIn){document.getElementById'ctl00_ContentPlaceHolder1_ctl00_HomePage_Agree').value = 'True';}"
ie.Document.parentWindow.execScript "window.ConfirmationPopup = " & f, "jscript"
ie.Document.getElementById("ctl00_ContentPlaceHolder1_ctl00_HomePage_SignIn").Click
回答by ARich
Try Application.SendKeys "{ENTER}", True
after your login code.
Application.SendKeys "{ENTER}", True
在您的登录代码后尝试。
Also, try adding
Do: DoEvents: Loop Until Not objIE.Busy
and
Do While objIE.readyState <> 4: DoEvents: Loop
any time you need a page to load. In my experience, coupling the two together works pretty well, whereas using just one or the other can sometimes produce errors and using Application.Wait
can slow down your code unecessarily.
此外,尝试添加
Do: DoEvents: Loop Until Not objIE.Busy
和
Do While objIE.readyState <> 4: DoEvents: Loop
任何时候你需要把一个页面加载。根据我的经验,将两者结合在一起效果很好,而仅使用其中一个有时会产生错误,并且使用Application.Wait
会不必要地减慢代码速度。