AutoIT IE登录时未查找对象

时间:2014-08-26 17:41:30

标签: javascript html internet-explorer autoit browser-automation

我让AutoIt使用我的路由器登录页面。但是对于我们的HP Pro曲线开关......我无法让它工作......

#NoTrayIcon
#include <ie.au3>
$Address = "http://10.255.96.2/index.html"
$pwd="mypassword"

$oIE = _IECreate ($Address)
_IELoadWait($oIE)
$oForm = _IEFormGetObjByName ($oIE, "maindata")

for $stuff in $oForm
if $stuff.name = "password" and $stuff.type = "password" then _IEFormElementSetValue ($stuff, $pwd)
If $stuff.value == "Login" and $stuff.type = "submit" Then $stuff.click
Next

exit

我得到“H:\ loginToSwitch.au3”(10):==&gt;变量必须是“对象”类型。:

基本上“maindata”是incorect。

如果我右键单击页面并查看源代码,那么这是HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<meta name=Copyright content="Copyright (c) 2005 HP Networks, Inc. All Rights Reserved.">
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta http-equiv="Pragma" content="no-cache">
<title>HP Networks Web Interface</title>
<script src="/mainjs.js" language=javascript type="text/javascript"></script>
<script language="javascript" type="text/javascript">
//<!--
checkFrame();   // Ensure we are not within a frame
implementLoginFrames('login.html');
document.write('<noframes>');
//-->
</script>
</head>

<body BGCOLOR="#DAE3EB">
<center>
<font color="#008BD1" face="Arial, Helvetica, sans-serif"><B>Please ensure that your browser supports Frames and Javascript, and that they are both enabled.</b></font>
</center>
<script language="javascript" type="text/javascript">
//<!--
document.write('<\/noframes>');
//-->
</script>
</body>
</html>

然而,当我使用firefox来识别元素时...它给了我一个完全不同的代码(这是我从中得到的按钮和文本框信息):

    <html>
    <head></head>
    <frameset framespacing="0" border="0" frameborder="0" rows="80,*">
        <frameset framespacing="0" border="0" frameborder="0" cols="*,22"></frameset>
        <frame src="login.html" noresize="" name="loginFrame" scrolling="auto">
            #document
                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
                <html>
                    <head></head>
                    <body onload="loadConfiguration();">
                        <script type="text/javascript" language="javascript"></script>
                        <form onsubmit="return login();" name="tF" method="post" action="login.html">
                            <table class="container" style="height: 650px">
                                <tbody>
                                    <tr>
                                        <td class="container">
                                            <div class="container">
                                                <div class="loginbox">
                                                    <!--

                                                     Login box HTML code here... 

                                                    -->
                                                    <table class="login">
                                                        <tbody>
                                                            <tr>
                                                                <td id="devname" class="hdr style2" style="color: #ffffff; font-family: Arial, Helvetica, sans-serif;" colspan="2"></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="hdr style2" valign="top"></td>
                                                                <td class="maindata">
                                                                    <input id="password" type="password" onkeypress="capsCheck(event);" size="20" name="password" maxlength="16"></input>
                                                                    <script type="text/javascript" language="javascript"></script>
                                                                    <input class="button" type="submit" style="position: relative; top: 2;" value="Login"></input>
                                                                    <br></br>
                                                                    <span id="passwd" class="default" style="display: none;"></span>
                                                                </td>
                                                            </tr>
                                                            <tr></tr>
                                                            <tr></tr>
                                                        </tbody>
                                                    </table>
                                                    <span id="wrong" class="warning" style="display: none;"></span>
                                                    <br></br>
                                                    <span id="capsmsg" class="warning" style="display: none;"></span>
                                                    <br></br>


                                                    It is recommended that you use a minimum of

                                                    <br></br>

                                                    Microsoft® Internet Explorer 5.5 to view the Inter…

                                                    <br></br>
                                                    <br></br>
                                                    <!--

                                                     Login box HTML end... 

                                                    -->
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <script type="text/javascript" language="javascript"></script>
                        </form>
                    </body>
                </html>
        </frame>
    </frameset>
    <noframes></noframes>
</html>

1 个答案:

答案 0 :(得分:0)

_IEFormGetObjByName()返回一个对象变量,而不是一个集合。

使用_IETagNameAllGetCollection($oForm)列出对象内的所有元素。

编辑:

我仔细查看了html,我发现密码字段有ID和NAME属性。使用_IEGetObjById获取密码字段的对象。

#NoTrayIcon
#include <ie.au3>
$Address = "http://10.255.96.2/index.html"
$pwd="mypassword"

$oIE = _IECreate ($Address)
;_IELoadWait($oIE) Not needed because IECreate does wait for page to load by default.
$oPassword = _IEGetObjById($oIE, "password")
_IEFormElementSetValue ($oPassword, $pwd)

$oForm = _IEGetObByName($oIE, "tF")
_IEFormSubmit($oForm)