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

C#下用HtmlDocument对象的GetElementById方法模拟登录QQ空间失败了,这咋办?

前些天的时候,QQ空间(http://qzone.qq.com/)可以用C#下HtmlDocument对象的GetElementById方法模拟登录QQ空间。就在最近几天,这个程序登录不上QQ空间了,查看了QQ空间的源文件才知道,腾讯已将QQ空间的代码作了改写且改动不小(之前QQ号、QQ密码输入框、登录按钮的id、name属性都是一目了然写在源文件的,现在源文件没有任何这类信息。按F12呼出开发人员工具倒是在其中找到了uinArea类(其中写有id为u)、pwdArea类(其中写有id为p)、login_button类(其中写有id为login_button)。请问我之前的这段代码如何作改动使之能够再次正常登录QQ空间呢?另外网页元素的诸如id、name属性以在哪里看到的为准呢?是查看-源文件中的还是F12开发人员工具中的?
附(之前能够正常运行的代码):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginQzone
{
    public partial class LoginQzoneForm : Form
    {
        public LoginQzoneForm()
        {
            InitializeComponent();
        }
        private void LoadLoginQzoneForm(object sender, EventArgs e)
        {
            this.webQzone.Navigate("http://qzone.qq.com/");
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.txtQQNumber.Text) || String.IsNullOrEmpty(this.txtPassword.Text))
            {
                MessageBox.Show("QQ号或密码不能为空!");
                return;
            }
            if (this.webQzone.ReadyState != WebBrowserReadyState.Complete)
            {
                MessageBox.Show("网页未加载完成,请稍后再试!");
                return;
            }
            LoginQzoneFunc();
        }
        private void LoginQzoneFunc()
        {
            HtmlDocument htmlDoc = this.webQzone.Document;
            HtmlElement qqNumber = htmlDoc.GetElementById("u");
            qqNumber.SetAttribute("value", this.txtQQNumber.Text);
            HtmlElement qqPassword = htmlDoc.GetElementById("p");
            qqPassword.SetAttribute("value", this.txtPassword.Text);
            HtmlElement btnLoginQzone = htmlDoc.GetElementById("login_button");
            if (btnLoginQzone != null)
            {
                btnLoginQzone.InvokeMember("click");
            }
        }
    }
}
--------------------编程问答-------------------- qqNumber.SetAttribute("value", this.txtQQNumber.Text); --------------------编程问答-------------------- 我也在参考你的代码?用这种控件事不是就得不到这种属性了! --------------------编程问答-------------------- 是不是要换其他的方式了 --------------------编程问答-------------------- 我也上空间看了下,应该是改用了frame框架,特别是iframe 元素会创建包含另外一个文档的内联框架(即行内框架)。所以,你的方法就不行了。 --------------------编程问答-------------------- 你应该用如下代码来获取:

WebBrowser1.Document.Window.Frames["login_frame"].Document

WebBrowser1.Document.Window.Frames["login_frame"].Document.GetElementById("u").InnerText = "12345678"

下面是我获取的框架网页源码:
<DIV style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" id=login class=main>
<DIV id=normal_login>
<DIV style="DISPLAY: none" id=qlogin></DIV>
<DIV id=web_login>
<FORM style="MARGIN: 0px" id=loginform onsubmit="if(!isAbleSubmit){return false;};return ptui_onLoginEx(loginform, 'qq.com')" method=post onreset="return onFormReset(loginform)" name=loginform action=http://ptlogin2.qq.com/login target=_self autocomplete="off">
<UL id=g_list>
<LI id=err_m class=err_m></LI>
<LI id=g_u><SPAN><U id=label_uin>QQ帐号:</U></SPAN><INPUT onblur="try{ptui_onUserBlue('u', '#CCCCCC');}catch(e){}check();" style="IME-MODE: disabled; COLOR: #cccccc" id=u class=inputstyle onfocus="try{ptui_onUserFocus('u', '#000000')}catch(e){}" tabIndex=1 value=QQ号码/手机/邮箱 name=u> <LABEL><A id=label_newreg tabIndex=7 href="http://zc.qq.com?from=pt" target=_top>注册新帐号</A></LABEL> </LI>
<LI id=g_p><SPAN><U id=label_pwd>QQ密码:</U></SPAN><INPUT id=p class=inputstyle onfocus=check(); tabIndex=2 value="" maxLength=16 type=password name=p> <LABEL><A id=label_forget_pwd tabIndex=8 onclick=onClickForgetPwd() href="http://ptlogin2.qq.com/ptui_forgetpwd" target=_top>忘了密码?</A></LABEL> </LI>
<LI style="DISPLAY: none" id=verifyinput><SPAN for="code"><U id=label_vcode>验证码:</U></SPAN><INPUT style="IME-MODE: disabled" id=verifycode class=inputstyle tabIndex=3 maxLength=5 name=verifycode> </LI>
<LI style="DISPLAY: none" id=verifytip><SPAN> </SPAN><U id=label_vcode_tip>请输入图中的字符不区分大小写</U> </LI>
<LI style="DISPLAY: none" id=verifyshow><SPAN for="pic"> </SPAN><IMG id=imgVerify width=130 onload=imgLoadReport() height=53><LABEL><A id=changeimg_link tabIndex=6 href="javascript:ptui_changeImg('qq.com', 15002005, true);">看不清,换一张</A></LABEL> </LI></UL>
<DIV class=login_button><INPUT id=login_button class=btn tabIndex=5 value="登 录" type=submit> </DIV>
<DIV id=label_unable_tips class=lineright> </DIV><INPUT value=15002005 type=hidden name=aid> <INPUT value=http://qz.qzone.qq.com type=hidden name=u1> <INPUT value=loginerroralert type=hidden name=fp> <INPUT value=1 type=hidden name=h> <INPUT value=1 type=hidden name=ptredirect> <INPUT value=2052 type=hidden name=ptlang> <INPUT value=1 type=hidden name=from_ui> <INPUT type=hidden name=dumy> </FORM></DIV><!--二维码登录区域入口 user_needqrlogin --><!--二维码登录区域入口结束 user_needqrlogin -->
<DIV style="DISPLAY: none" id=switch class=lineright><A onclick=""></A></DIV>
<DIV style="CLEAR: both"></DIV></DIV><!--二维码登录区域 user_needqrlogin --><!--二维码登录区域 end --></DIV>
<SCRIPT type=text/javascript>
var g_f=$("loginform");
if((g_appid==46000101)&&(g_f.u.value=="QQ号码/手机/邮箱")){
g_f.u.value="支持微博帐号/QQ号码/邮箱地址";
var wb_tips=document.createElement("li");
wb_tips.style.paddingTop="0";
wb_tips.style.color="gray";
wb_tips.id="wb_tips";
wb_tips.innerHTML="<span> </span> "+"未登录QQ或者浏览器不支持快速登录";
$("u").value="";
$("u").color="";
$("g_list").insertBefore(wb_tips,$("g_p"));
}
g_time.time4=new Date();
</SCRIPT>
<!--0-->
<SCRIPT>onSelectLoad();</SCRIPT>

<SCRIPT type=text/javascript src="http://imgcache.qq.com/ptlogin/ver/10043/js/comm.js?ptui_identifier=000E012E3A89FB8098A4F8D0E9B21E4FF2947BF9AD24272D7C18A9AC29"></SCRIPT>

<SCRIPT type=text/javascript>
function cleanCache(f){
var t=document.createElement("iframe");
if(f.split("#").length == 3) f = f.substring(0,f.lastIndexOf("#"));
t.src = f;
t.style.display = "none";
document.body.appendChild(t);
}
function loadScript(src,callback) {   
var tag = document.createElement("script");
tag.onload = tag.onreadystatechange = function() {
if ( !this.readyState ||this.readyState === "loaded" || this.readyState === "complete") {
if(typeof callback=="function"){
callback();
}
tag.onload = tag.onreadystatechange = null;
}
};
tag.onerror=function(){
tag.onerror=null;
callback();
}
tag.src = src;
document.getElementsByTagName("head")[0].appendChild(tag);
};
//comm.js拉去失败
if(typeof(ptuiCB)=="undefined"){
var imgAttr2 = new Image();
imgAttr2.src = location.protocol+"//ui.ptlogin2.qq.com/cgi-bin/report?id=85289&union=256039";
var serverJsPath="../js/"+window.ptui_version+"/comm.js?max_age=604800&ptui_identifier=000E012E3A89FB8098A4F8D0E9B21E4FF2947BF9AD24272D7C18A9AC29";
loadScript(serverJsPath,function(){
if(typeof(ptuiCB)=="undefined"){
var button=document.getElementById("login_button");
button&&(button.disabled=true);
imgAttr2.src = location.protocol+"//ui.ptlogin2.qq.com/cgi-bin/report?id=299500";
}     
});
}

onPrePageLoad();
if(reloadweb){
loadweblogin();
}
function onPageClose(){ptui_notifyClose();}
function ptuiV(v){
if (v!=g_version){
cleanCache("/clearcache.html#"+location.href);
}
}
function checkVersion(){
var t = document.createElement("script");
t.src = "/cgi-bin/ver";
document.body.appendChild(t);
}
setTimeout(checkVersion,1500);
</SCRIPT>
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,