Respond to Website Message Box by Automating InternetExplorer with VBA

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

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

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.

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.

回答1:

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; 

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!