通过使用VBA自动化InternetExplorer来响应网站消息框 [英] Respond to Website Message Box by Automating InternetExplorer with VBA

查看:185
本文介绍了通过使用VBA自动化InternetExplorer来响应网站消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel VBA内部进行自动化,部分是

I'm automating ie from within Excel VBA like this, in part

Set ie = CreateObject("InternetExplorer.Application")

With ie
    .navigate "http://www.statspass.com/login.asp"
    Do While .Busy: DoEvents: Loop
    Do While .readyState <> 4: DoEvents: Loop

    With .document.forms(0)
        .txtUsername.Value = "username"
        .txtPassword.Value = "*****"
        .submit
    End With

稍后在代码中,我导航到其他URL并获取一些数据.昨天一切都很好.今天,当到达提交"行时,该网站显示一个消息框,显示另一个会话正在注销".我设置了ie.Visible = True,并逐步执行了代码以弄清楚这一点.按照书面说明,不会显示消息框.当显示消息框时,它位于页面上,并且.Busy(上面未显示)始终为true,因此代码只是循环存在.

Later in the code, I navigate to a different url and get some data. This all worked fine yesterday. Today, when it gets to the "submit" line, the website displays a message box that says "Another session is being logged off". I set ie.Visible = True and stepped through the code to figure that out. As written, the message box isn't shown. When the message box is shown it sits on the page and the .Busy (not shown above) is always true so the code just sits there in a loop.

当ie.Visible = False时,没有消息框(正如我期望的那样),代码没有保留,但是在下一个Navigate方法调用中,它最终返回登录页面(因为不是正确登录),并且尝试访问登录屏幕上不存在的某些表时出现错误.

When ie.Visible = False, there is not a message box (as I would expect), the code is not held up, but on the next Navigate method call, it ends up back at the login page (because it's not properly logged in yet) and I get an error trying to access certain tables that don't exist on the login screen.

我需要登录到该网站,导航到某个页面,并从表格中获取信息.根据我的代码现在所在的位置,我想弄清楚如果存在该消息框,则对该消息框说确定".但是,如果我需要退后一步,考虑其他方法,我并不反对.

I need to login to this website, navigate to a certain page, and get information from a table. Based on where my code is right now, I want to figure out how to say OK to that message box if and when it exists. But if I need to step back and consider alternative methods, I'm not averse to that.

不太重要,但仍然困扰我,这就是为什么这段代码昨天起作用了吗?我对其进行了测试,并将其发送给报告了这些错误的客户,今天我得到了这些错误.

Less important, but still bugging me, is why this code worked yesterday? I tested it, sent it to a client who reported these errors, and today I'm getting the errors.

推荐答案

在遇到类似情况时,我可以通过执行JavaScript来执行以下操作,以解决该问题:将JavaScript替换为仅返回true的确认函数,然后执行会触发警报的操作.

When I have encountered similar situations, I was able to get around the problem by executing JavaScript to replace the confirm function with one that simply returns true, prior to performing the action that will invoke the alert.

oldConfirm = window.confirm; 
window.confirm = function (msg) { return true; };

如果您希望将所有内容放回原处,则可以执行以下操作...

If you want to put everything back as it was afterward, you can then execute the following...

window.confirm = oldConfirm; 
oldConfirm = undefined; 

关于为什么这是必要的,当您的代码之前没有它时,恐怕我一无所知.不过,我始终需要在Rails/Cucumber/Selenium/Firefox中使用它.

As to why this should be necessary, when your code worked without it before, I'm afraid I have no clue. I always need this with Rails/Cucumber/Selenium/Firefox though.

这篇关于通过使用VBA自动化InternetExplorer来响应网站消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆