开发者

Respond to Website Message Box by Automating InternetExplorer with VBA

开发者 https://www.devze.com 2023-02-03 23:04 出处:网络
I\'m automating ie from within Excel VBA like this, in part Set ie = CreateObject(\"InternetExplorer.Application\")

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"
 开发者_Go百科   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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消