当前位置:编程学习 > C#/ASP.NET >>

想实现网页自动填写用户名密码WebBrowser的问题

我想实现自动填用户名密码,并且提交的程序,以前再其他网页上做过,而且成功了。但是遇到另一个网页就不好使了,得到的vDoc.All.length=0,vDoc.all为空
我的程序代码如下:
 Dim vDoc, vTag

        Dim i As Integer

        vDoc = WebBrowser1.Document

        For i = 0 To vDoc.All.length - 1                 '检测所有标签 

            If UCase(vDoc.All(i).tagName) = "INPUT" Then           '找到input标签 

                vTag = vDoc.All(i)

                If vTag.Type = "text" Or vTag.Type = "password" Then                    

                    Select Case vTag.Name       '按标签的名字,对号操做 

                        Case "Ecom_User_ID"

                            vTag.Value = "zhanghao"      '这里写入你的用户名
                        Case "Ecom_Password"

                            vTag.Value = "123456"       '这里写入你的密码 

                    End Select



                End If

            End If


        Next i


        WebBrowser1.Document.forms(0).submit()    '提交按钮





这个是网页源代码

<script language="javascript">
if(top.isLogin == "false")
{
top.mainStart();
}
</script>
<script type="text/javascript" src="/epm/static/login/js/pngfix.js"></script>
</head>

<body onload="init();" scroll="no">
<form action="http://10.160.231.186:80/epm/login" method="post" name="loginForm" id="loginForm" onsubmit="return checkAll();">
<input type="hidden" name="style" value="default" />
<input type="hidden" name="r" value="login" />

<table align="center" cellSpacing="0" cellPadding="0" border="0" style="width:100%; height:100%" >
<tr>
<td class="topbg">
</td>
</tr>
<tr>
<td  class="tablebg" align="center" valign="middle">
<table cellSpacing="0" cellPadding="0" border="0">
<tr>
<td>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<table id="loginMainStyle" cellSpacing="0" cellPadding="0" border="0" class="loginMain">
<tr style="height:22px">
<td>
</td>
</tr>
<tr style="height:32px">
<td align="right" class="errortd">
<table cellSpacing="0" cellPadding="0" border="0" class="errorbg" id="errorMessage" style="display:none">
<tr>
<td class="errorImg">
<img src="images/error.png" border=0></img>
</td>
<td class="inputTdRight">

</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="height:155px">
</td>
</tr>
<tr>
<td style="height:120px" align="right">
<table cellSpacing="0" cellPadding="0" border="0" height="100%">
<tr>
<td class="text">
用户名 :
</td>
<td colspan="2" class="inputTd">
 <input class="userInput" name="username" tabindex="1" maxLength="30" size="19" title="请填写用户名" onkeypress="moveFocus(this)" style="width:170px" />
</td>

 <td rowspan="2" class="buttonTd">
<input class="button" type="submit" id="logonButton" tabindex="4" value=""/>
 </td>

</tr>
<tr>
<td class="text">
密   码 :
</td>
<td colspan="2" class="inputTd">
 <input class="pwdInput" name="password" tabindex="2" maxLength="30" size="19" title="请填写用户密码" type="password" onkeypress="moveFocus(this)" style="width:170px" AUTOCOMPLETE="off"/>
</td>

</tr>

<tr>
<td>
</td>
<td colspan="2" class="checkboxTd">
<input type="checkbox" name="checkcookie"><span class="text">在此计算机上保存我的信息。</span>
</td>
<td>
</td>
</tr>

</table>
</td>
</tr>
<tr style="height:45px">
<td>
</td>
</tr>
<tr>
<td style="height:33px">
<table cellSpacing="0" cellPadding="0" border="0" height="100%">
<tr>
<td class="downloadTd">
<div class="download" onmouseover="f_downOver(this)" onmouseout="f_downOut(this)" onclick="f_download(this)"></div>
</td>
<td>
<div class="add" onmouseover="f_addOver(this)" onmouseout="f_addOut(this)" onclick="f_add(this)"></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr style="height:32px">
<td align="right" class="copyright">
 2008年版权所有 国家电网公司
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="37px">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="bottombg">
</td>
</tr>
</table>
</form>
--------------------编程问答--------------------
<script language="javascript">
<!--
var loginMessage = "";
var shortAppName =  "ln";
var logoArray = ["hl","bj"];// ["hl","ln","bj","ha","jx","sn","md","sc","xj"];

if(loginMessage == "success")
{
top.startFrameSet();
}

if(top.document.all("mianFrame")== null)
{
window.location = "../../index.htm";
}

if(loginMessage == "changePassword")
{
var strURL = "http://10.160.231.186:80/epm/static/login/changePassword.jsp?username=" + "";
var sFeature = "center:yes;dialogWidth:481px;dialogHeight:379px;";
var dlgWin = window.showModalDialog(strURL, "", sFeature);
}

if(loginMessage == "会话已存在,请关闭所有浏览器再重新登录!")
{
var strURL = "http://10.160.231.186:80/epm/static/errorpage/error-session-repeat.jsp?username=" + "";
var sFeature = "center:yes;dialogWidth:481px;dialogHeight:379px;";
var dlgWin = window.showModalDialog(strURL, "", sFeature);
}

function init() 
{
//add by wangfei for province logo style
initMainLogStyle();
if(loginMessage != "changePassword")
{
    setFocus();  
    var checkcookie=document.getElementsByName("checkcookie")[0];   
    if(getCookie("username") != null && getCookie("username").length > 0)
    {
        checkcookie.checked = true;        
    }
    else
    {
        checkcookie.checked = false;        
    }   
    if(loginMessage != "")
    {
     document.all("errorMessage").style.display = "";
    }
    if(top.document.all("mianFrame").src == "")
{
top.document.all("mianFrame").src = "innermain.html?version=1";
top.document.all("frmJs").src = "page_js.html";
}
    }
}
function initMainLogStyle()
{
if(!shortAppName)
{
return;
}
for(var _index in logoArray)
{
if(shortAppName==logoArray[_index])
{
document.getElementById("loginMainStyle").className ="login_main_"+shortAppName;
break;
}
}
}


function checkAll() 
{
    var i;
    var ele;
    var loginForm=document.getElementById("loginForm");
    for (i=0;i<loginForm.elements.length;i++) {
        ele = document.all? loginForm.elements(i):loginForm.elements[i];
        if (ele != null) {
            if (!checkField(ele)) return false;
        }
    }
    var checkcookie=document.getElementsByName("checkcookie")[0];
    var username=document.getElementsByName("username")[0];
    if(checkcookie.checked) {
        setCookie("username", username.value,30);
    } else {
        setCookie("username", "");
    }
}

function checkField(field) 
{
    if (field.type != "text" && field.type != "password") return true;
    if (field.getAttribute("value") == "") {
        alert(field.getAttribute("title") + "!" );
        field.focus();
        return false;
    }
    return true;
}

function moveFocus(field) 
{   
    var event=window.event||arguments[0];
    if (event.keyCode == 13) {
      if (field.name == "password") {
          var authnumber=document.getElementsByName("authnumber")[0];
          if (authnumber) {
          authnumber.focus();
          if(window.event){
           event.returnValue = false;
          }else event.preventDefault();
          
        }
      }
      else if (field.name == "username") {
          var password=document.getElementsByName("password")[0];
          password.focus();
          if(window.event){
           event.returnValue = false;
      }else event.preventDefault();
      }
    }
}

function setFocus() 
{   
var loginForm=document.getElementById("loginForm");
var username=document.getElementsByName("username")[0];
    username.value = getCookie("username");
    username.focus();
    username.select();
}

function setCookie(name, value, expires)
 {
    if (!isNaN(parseFloat(expires))) 
    {
        var expDate = new Date();
        expDate.setTime( expDate.getTime() + parseFloat(expires)*24*60*60*1000);
        var cookieExpires = expDate.toGMTString();
        document.cookie = name + "=" + escape(value) + ";expires=" + cookieExpires + ";";
    } 
    else
    {
        document.cookie = name + "=" + escape(value) + ";";
    }
}

function getCookie(name) 
{
    var arg  = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while(i < clen ) 
    {
        var j = i + alen;
        if( document.cookie.substring(i,j) == arg ) 
        {
            var endstr = document.cookie.indexOf(";",j);
            if( endstr == -1 )
                endstr = document.cookie.length;
            return unescape( document.cookie.substring(j, endstr) );
        }
        i = document.cookie.indexOf(" ",i) + 1;
        if( i == 0 ) break;
    }
    return "";
}

function f_downOver(obj)
{
obj.className = "download_over";
}

function f_downOut(obj)
{
obj.className = "download";
}

function f_addOver(obj)
{
obj.className = "add_over";
}

function f_addOut(obj)
{
obj.className = "add";
}

function f_add()
{
window.external.addFavorite(window.location,document.title);
}

function f_download()
{
var strURL='/epm/download/download.jsp';
sFeature = "center:yes;location:no;status:no;dialogWidth:482px;dialogHeight:332px;";
var dlgWin = window.showModalDialog(strURL, '', sFeature);
return dlgWin;
}

document.onkeydown = function()
{
if(event.keyCode == 9)
{
if(event.srcElement.type == null)
{
event.keyCode = 0;
event.returnValue=false;  
}
}
}

-->
</script>

</body>
</html>






但是用WebBrowser1.Document.body.createtextrange().htmltext
得到的却是



--------------------编程问答-------------------- <IFRAME id=frmJs height=0 marginHeight=0 src="page_js.html" frameBorder=0 
width=0 marginWidth=0 scrolling=no></IFRAME>
<SCRIPT type=text/javascript src="/epm/static/ria/dojo/dojo-unieap.js" 
djConfig="parseOnLoad: false"></SCRIPT>

<SCRIPT onreadystatechange="if(this.readyState=='complete'){dojo._loadInit();}" 
defer src="//:"></SCRIPT>

<SCRIPT type=text/javascript src="/epm/static/ria/unieap/ds.js"></SCRIPT>

<SCRIPT type=text/javascript src="/epm/static/ria/ep/dialog/Dialog.js"></SCRIPT>

<SCRIPT type=text/javascript src="/epm/static/ria/ep/rpc.js"></SCRIPT>

<SCRIPT type=text/javascript src="/epm/static/ria/ep/cache.js"></SCRIPT>

<SCRIPT language=javascript 
src="/epm/static/common/base/Application.js"></SCRIPT>

<SCRIPT language=javascript src="/epm/static/common/base/loading.js"></SCRIPT>

<SCRIPT language=javascript src="/epm/static/extension/constant.js"></SCRIPT>

<SCRIPT language=javascript src="main.js"></SCRIPT>

<SCRIPT language=javascript src="main_extend.js"></SCRIPT>

<SCRIPT language=javascript src="pageleftmove.js"></SCRIPT>

<SCRIPT language=javascript>
dojo.addOnLoad(main_init);
</SCRIPT>
<IFRAME style="Z-INDEX: 1" id=loginFrame height="100%" marginHeight=0 
src="../login/login.jsp" frameBorder=0 width="100%" marginWidth=0 
scrolling=no></IFRAME><IFRAME style="Z-INDEX: -1" id=mianFrame height="100%" 
marginHeight=0 src="innermain.html?version=1" frameBorder=0 width="100%" 
name=mianFrame marginWidth=0 scrolling=no></IFRAME></META><LINK rel=stylesheet 
type=text/css href="../theme/default/dialog.css"><LINK rel=stylesheet 
type=text/css href="../theme/default/loading.css">
<TABLE style="DISPLAY: none" id=innerTable border=0 cellSpacing=0 cellPadding=0 
width="100%" height="100%">
<TBODY>
<TR id=headDiv height=23>
<TD>
<TABLE class=x-window-tc border=0 cellSpacing=0 cellPadding=0 width="100%" 
vlign="top">
<TBODY>
<TR>
<TD style="WIDTH: 5px" class=x-window-tl>  </TD>
<TD id=headTitle class=dialog-title></TD>
<TD id=headButton align=right>
<TABLE border=0 cellSpacing=0 cellPadding=0 height=24 alirn="right">
<TBODY>
<TR>
<TD><SPAN id=collapseImg class=dialog-minimize> </SPAN> <SPAN 
style="DISPLAY: none" id=expandImg class=dialog-maximize> </SPAN> <SPAN 
id=maxImg class=dialog_max_base> </SPAN> <SPAN style="DISPLAY: none" 
id=minImg class=dialog_min_base> </SPAN> </TD>
<TD><SPAN id=closeImg class=dialog-close> </SPAN> 
</TD></TR></TBODY></TABLE></TD>
<TD class=x-window-tr>  </TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD>
<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%" height="100%">
<TBODY>
<TR>
<TD style="WIDTH: 5px" class=x-window-ml>  </TD>
<TD>
<DIV id=bodyDiv class=dialogBody valign="center"></DIV></TD>
<TD style="WIDTH: 5px" class=x-window-mr>  
</TD></TR></TBODY></TABLE></TD></TR>
<TR id=footerHr height=34>
<TD>
<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%" height="100%">
<TBODY>
<TR>
<TD style="WIDTH: 5px" class=x-window-ml>  </TD>
<TD id=footerDiv class=buttonbg align=right></TD>
<TD style="WIDTH: 5px" class=x-window-mr>  
</TD></TR></TBODY></TABLE></TD></TR>
<TR height=5>
<TD>
<TABLE class=x-window-bc border=0 cellSpacing=0 cellPadding=0 width="100%" 
height=5>
<TBODY>
<TR>
<TD style="WIDTH: 5px" class=x-window-bl></TD>
<TD class=x-window-bc>
<TABLE border=0 cellSpacing=0 cellPadding=0 height=5>
<TBODY>
<TR>
<TD style="FONT-SIZE: 1px"> </TD></TR></TBODY></TABLE></TD>
<TD style="WIDTH: 5px" 
class=x-window-br></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<DIV 
style="FILTER: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=50); DISPLAY: none" 
id=_moveDiv class=x-dlg-proxy></DIV>
<DIV style="DISPLAY: none" id=progress valign="middle"><SPAN 
class=progress-panel><SPAN class=progress-bar align="left"><SPAN 
style="WIDTH: 0px" id=progressBar class=p1></SPAN></SPAN></SPAN><SPAN 
id=progressText></SPAN></DIV>
<SCRIPT language=javascript>
var dialogs = [];
var closing = false;
showMask = function()
{
_loading.showMask();
}


hideMask = function()
{
_loading.hideMask();
}

showMask_top = function()
{
_loading.showMask_top();
}


hideMask_top = function()
{
_loading.hideMask_top();
}

showLoading = function(info)
{
_loading.showLoading(info);
}

hideLoading = function()
{
_loading.hideLoading();
}

function Progress(total)
{
var _total = total;
this.updateProgress = function(step)
{
if(step > _total) return;
if(step == _total)
{
document.getElementById("progress").style.display = "none";
hideMask();
}
else
{
if(step == 1)
{
showMask();
}
var per = Math.ceil(step * 100 /_total)
document.getElementById("progress").style.display = "block";
var bar = document.getElementById("progressBar").style.width = per + "%";
document.getElementById("progressText").innerText = per + "%";
}
}
}

function createDialog(config)
{
return new ep.dialog.Dialog(config);
}

function addDialog(dialog)
{
dialogs.push(dialog);
}
function getDialogs()
{
return dialogs;
}
function removeDialog(idx)
{
dialogs.splice(idx, 1);


function showErroeDetail()
{
if(document.all("error_detail_img").className == "error_detail_down")
{
_application.getDialog(true).setHeight(500);
document.all("error_detail").style.display = "";
document.all("error_detail_img").className = "error_detail_up";

}else
{
document.all("error_detail_img").className = "error_detail_down";
_application.getDialog(true).setHeight(150);
document.all("error_detail").style.display = "none";
}
}

document.onkeydown = function()
{
if((event.srcElement.tagName != "INPUT" && event.srcElement.tagName != "TEXTAREA") || (event.srcElement.tagName == "INPUT" && (event.srcElement.type == "checkbox" || event.srcElement.type == "button")))
{
  if(event.keyCode==8 || event.keyCode==166){   
{   
     event.keyCode=0;   
     event.returnValue=false;}   
}
}
}

</SCRIPT>

<DIV style="POSITION: absolute; DISPLAY: none; TOP: 0px; LEFT: 179px" 
id=middlebgMove class=middlebg></DIV>
<DIV style="DISPLAY: none">
<FORM method=post name=form_component action=/epm/component.dox><INPUT 
type=hidden name=name> <INPUT type=hidden name=event> <INPUT type=hidden 
name=parameter> <INPUT type=hidden name=data> <INPUT type=hidden name=type> 
<INPUT type=hidden name=writer> <INPUT type=hidden name=identifier> 
</FORM></DIV>



请问这种情况如何解决?
有没有办法直接提交用户名密码到网页,实现登录呢?或者其他办法,谢谢各位了

补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,