Excelmacroslogin网站时出错

我有一个macros来打开IE浏览器,login到一个页面,并拉一个表。 这是为我们的路由器,而不是一个网站。 它给我的错误“对象variables或块variables未设置”。 它在debugging器中突出显示黄色的“用户名”行。

这是来自网站的代码部分:

<form name="login_form" method="post" style="display:none"> <input type="hidden" name="mode" value="submit" /> <input type="hidden" name="option" value="login" /> <div id="login_box"> <div id="company_name">Copyright &copy; <span id="company_name_text"></span>. All rights reserved.</div> <div id="login_box_layout"> <strong>Login</strong><br><br> Username:<br> <input type="text" class="text_box" name="username" tabindex="1" autocapitalize="off"><br><br> Password:<br> <input type="password" class="text_box" name="password" tabindex="2" autocomplete="off"><br><br> <input type="button" value="Login" tabindex="3" class="submit_action"> <div class="smart_status" id="smart_status"></div> </div> </div> </form> 

这是macros观的:

 Sub GetTable() Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True .Navigate "http://192.168.1.1" Do While ie.Busy: DoEvents: Loop Do Until .ReadyState = 4 DoEvents Loop .Document.all.Item("username").Value = "admin123" .Document.all.Item("password").Value = "password123" .Document.forms(0).submit End With 'now that we're in, go to the page we want ieApp.Navigate "http://192.168.1.1/cgi-bin/MANGA/index.cgi?mode=config&option=utlog" Do While ieApp.Busy: DoEvents: Loop Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop 'get the table based on the table's id Set ieDoc = ieApp.Document Set ieTable = ieDoc.all.Item("device_log") 'copy the tables html to the clipboard and paste to teh sheet If Not ieTable Is Nothing Then Set clip = New DataObject clip.SetText "" & ieTable.outerHTML & "" clip.PutInClipboard Sheet1.Select Sheet1.Range("A1").Select Sheet1.PasteSpecial "Unicode Text" End If End Sub 

这是完整的页面源代码:

 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="apple-itunes-app" content="app-id=582215646, affiliate-data=, app-argument="> <meta http-equiv="X-UA-Compatible" content="IE=Edge"/> <link rel="shortcut icon" href="connicon.cgi?type=favicon"> <title>Web Admin | Welcome</title> <style> body { font-family:Arial, Helvetica, sans-serif; } #warning { margin:110px auto 10px; padding:30px; width:300px; background:#FCC; color:red; font-size:12px; } #login_box { position:relative; margin:80px auto 0; width:834px; height:434px; background:url("connicon.cgi?type=login_screen") no-repeat; } #login_box_layout { position:relative; margin:0 auto 0; padding:140px 0 0; width:250px; } .text_box { width:200px; } .smart_status { border:1px solid #FFCC56; color:#157015; font-weight:700; background:#FFFFB0; padding:5px 3px 3px 6px; margin:10px auto 0; } #company_name { position:absolute; top:385px; left:22px; border-radius:0 0 3px 3px; padding:5px; width:780px; background:#E9F1FC; font-size:9px; } div.notif_box { position:absolute; top:22px; left:22px; border-radius:8px 8px 0 0; padding:10px; height:85px; width:770px; background: black; color:red; font:700 24px Arial, Helvetica, sans-serif; text-align:center; } </style> <!-- jQuery UI - Style Sheet --> <link rel="stylesheet" href="../../jquery-ui.css?$Name: Release-8_4_119_PATCH1 $" type="text/css" /> <!-- <link rel="stylesheet" href="../../jquery-ui-custom.css?$Name: Release-8_4_119_PATCH1 $" type="text/css" /> --> <script src="../../MANGA/jquery.js?$Name: Release-8_4_119_PATCH1 $" type="text/javascript"></script> <!-- jQuery UI - Core --> <script type="text/javascript" src="../../MANGA/jquery.ui.core.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.widget.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.button.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.mouse.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.position.js?$Name: Release-8_4_119_PATCH1 $"></script> <!-- jQuery UI - Dialog --> <script type="text/javascript" src="../../MANGA/jquery.ui.draggable.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.resizable.js?$Name: Release-8_4_119_PATCH1 $"></script> <script type="text/javascript" src="../../MANGA/jquery.ui.dialog.js?$Name: Release-8_4_119_PATCH1 $"></script> <script src="../../MANGA/global.js?$Name: Release-8_4_119_PATCH1 $" type="text/javascript"></script> <script src="../../MANGA/validate_scripts.js?$Name: Release-8_4_119_PATCH1 $" type="text/javascript"></script> <script type="text/javascript" src="../../MANGA/menulib.js?$Name: Release-8_4_119_PATCH1 $"></script> </head> <body> <noscript> <div id="warning">You must enable Javascript to access the Web Admin.</div> </noscript> <form name="login_form" method="post" style="display:none"> <input type="hidden" name="mode" value="submit" /> <input type="hidden" name="option" value="login" /> <div id="login_box"> <div id="company_name">Copyright &copy; <span id="company_name_text"></span>. All rights reserved.</div> <div id="login_box_layout"> <strong>Login</strong><br><br> Username:<br> <input type="text" class="text_box" name="username" tabindex="1" autocapitalize="off"><br><br> Password:<br> <input type="password" class="text_box" name="password" tabindex="2" autocomplete="off"><br><br> <input type="button" value="Login" tabindex="3" class="submit_action"> <div class="smart_status" id="smart_status"></div> </div> </div> </form> </body> </html> <script> var r0t1e=function(s){return s.toLowerCase().replace(/[az]/g,function(c){var v="m";return String.fromCharCode(c.charCodeAt(0)-13*(c>v?1:-1))});},r0tle= ["~!@#$%^&*()_+","cnggba","nenxavf","`{}|[]\;:<>?,./"]; $(function() { var l=window,I=l.r0t1e,I=I(l.oemid),l=0>$.inArray(I,l.r0tle); $("body").toggleClass(I,!l).css({"visibility":"visible"}); }); $(function() { var is_cookies_supported = (function() { var supported = false; n = "__cookies_test__"; v = 100000000 + Math.floor(Math.random() * 900000000); setCookie(n, v); if (getCookie(n) == v) { clearCookie(n); supported = true; } return supported; })(), w = window, o = $("body"); // Ensure the Cookies is available, or we will block the login if (!is_cookies_supported) { o.empty().append( $("<div/>", { id: "warning" }).text("You must enable Cookies to access the Web Admin.") ); return; } init_smart_status(status_message); if ($("body").hasClass("nenxavf")) { return nenxavf(); } o.find("form[name=login_form]") .data("form_fields", [ "username", "password" ]) .on("validate", function() { var form = $(this), is_valid = true; $.each(form.data("form_fields") || [], function(_, s) { var o = form.find("[name=\"" + s + "\"]"); o.val(trim(o.val())); if (isempty(o.val())) { is_valid = false; return err(null, o); } }) if (is_valid) { form.get(0).submit(); } }) .on("keydown", "input", function(e) { if (e.which == 13) { $(e.delegateTarget).triggerHandler("validate"); return false;} }) .on("click", ".submit_action", function(e) { $(e.delegateTarget).triggerHandler("validate"); }) .find("#login_box").append(w.has_notification? $("<div/>").addClass("notif_box").append(w.notification_msg): "").end() .find("#company_name_text").html(init_company_name).end() .show() .find("input:text:first").focus().end(); }); </script> <style> body.nenxavf .ui-widget { font:10pt Arial; } </style> <script> function nenxavf() { $("<form/>").append( $("<input/>", { type: "hidden", name: "func" }).val("login"), "The server requires a username and password<br><br>", $("<span/>").css({ display: "inline-block", width: "80px" }).text("Username: "), $("<input/>", { name: "username" }), "<br>", $("<span/>").css({ display: "inline-block", width: "80px" }).text("Password: "), $("<input/>", { name: "password", type: "password" }), "<br><br>" ) .data("form_fields", [ "username", "password" ]) .data("retries", 3) .on("validate", function() { var form = $(this), is_valid = true; $.each(form.data("form_fields") || [], function(_, s) { var o = form.find("[name=\"" + s + "\"]"); o.val($.trim(o.val())); if (!o.val().length) { o.focus(); is_valid = false; } return is_valid; }) if (is_valid) { $.ajax("api.cgi", { type: "POST", cache: false, data: $(this).serialize(), context: this }) .then(function(json, status, jqXHR) { if (!(json && json["stat"] == "ok")) { return $.Deferred() .rejectWith(this, [ jqXHR, status, "Invalid Username or Password" ]) .promise(); } return jqXHR; }) .fail(function(jqXHR, status, msg) { var cls = "__err__", form = $(this), retry = +form.data("retries")-1; if (retry > 0) { form .data("retries", retry) .find("."+cls).remove().end() .append($("<div/>").css({ color: "red" }).addClass(cls).text(msg)) .find("[name=username]").val("").end() .find("[name=password]").val("").end(); } else { form.dialog("close"); } }) .done(function() { goto_menu("mainpage"); }); } }) .on("after_close", function() { $("body").empty().append( $("<div/>").css({ font: "bold 20pt Arial" }).text("Authorization Required"), "<br>", "This server could not verify that you are authorized to access the document requested.", "<br><br>", "<hr>" ); }) .on("keyup", "input", function(e) { if (e.which == 13) { $(e.delegateTarget).triggerHandler("validate"); } }) .dialog({ modal: true, title: "Authentication Required", close: function() { var me = $(this); me.triggerHandler("after_close"); }, buttons: [ { text: "Log In", click: function() { $(this).triggerHandler("validate"); } }, { text: "Cancel", click: function() { return $(this).dialog("close"); } } ] }) } </script> <script> var status_message = "", oemid = "PEPLINK", init_company_name = "Peplink", has_notification = false, notification_msg = ""; </script> 

我以不同的方式解决了这个问题,使用“SendKeys”并暂停。 这工作正常。

  Dim ie As Object Dim dftname As String Dim dftpass As String Set ie = CreateObject("InternetExplorer.Application") dftname = "username" dftpass = "password" With ie .Visible = True .Navigate "http://192.168.1.1/cgi-bin/MANGA/index.cgi?mode=config&option=logout" Do While ie.Busy: DoEvents: Loop Do Until .ReadyState = 4 DoEvents Loop End With Application.Wait (Now + TimeValue("0:00:02")) SendKeys dftname SendKeys "{Tab}" Application.Wait (Now + TimeValue("0:00:01")) SendKeys dftpass SendKeys "{Enter}" Application.Wait (Now + TimeValue("0:00:01"))