C# 获取网页信息问题

sonicrang 2010-09-08 11:35:18
<input type="text" class="normal" name='staticusername' id='staticusernameid' />
</li>
<li>
<label>密码</label>
<input type="password" class="normal" name='staticpassword' id='staticpasswordid' />

在这段html中我用下面的方法获取输入框的id,但是却不成功。
HtmlDocument doc = webBrowser1.Document;
foreach (HtmlElement em in doc.All)
{
string str = em.Id;
if (str == "staticusername")
{
em.SetAttribute("value", "abc"); break;

}
}



换了163邮箱的登录页面,
<input id="pwdInput" tabindex="2" class="ipt-t" type="password" name="password" onMouseOver="fEvent('mouseover',this)" onFocus="fEvent('focus',this)" onBlur="fEvent('blur',this)" onMouseOut="fEvent('mouseout',this)"/>

再用那个方法就可以了,发现是 ID的问题 :

id="pwdInput"

id = 'staticusename'

单引号和双引号的问题。求解,怎么获取这种单引号的id。


另外,像<input type="submit" value="登录"/>
这种没有id,没有name的按钮怎么获取。
...全文
438 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jesse_Fu 2011-06-09
  • 打赏
  • 举报
回复
内层的frame里的内容来从不同的受信站点,貌似这样就取不到,我也遇到同样的问题。
sonicrang 2010-09-09
  • 打赏
  • 举报
回复
源代码中
<form name='staticlogin' method='POST' action='https://211.139.92.253:7090/do_login.php' onSubmit='return checkField(this)'> 
<div class="loginContent">
<h2>静态密码登录</h2>
<p>适合校园套餐用户使用</p>
<ul class="login">
<li>
<label>用户名</label>
<input type="text" class="normal" name='staticusername' id='staticusernameid' />
</li>
<li>
<label>密码</label>
<input type="password" class="normal" name='staticpassword' id='staticpasswordid' />
<!--a href='#' onClick="modify_passwd()" >忘记密码?</a-->
<a name="linknamereturnpwd" target='_blank' href='' onClick="returnpwd(this)" >忘记密码?</a>
</li>
<!--li>
<input type="checkbox" name="staticsaveuserinfo" id="staticsaveuserinfoid" value="checkbox"/>
开通自动登录功能(暂不向标准资费用户开放)
</li-->

<li class="loginCtrl">
<input type="submit" value="登录"/>
</li>
</ul>
</div>
<input type='hidden' name='loginmode' value='static'>
<input type='hidden' name='wlanacip' value='218.203.208.4'>
<input type='hidden' name='wlanacname' value='1099.0931.931.00'>
<input type='hidden' name='wlanuserip' value='10.10.64.79'>
<input type='hidden' name='wlanacssid' value=''>
<input type='hidden' name='from_location' value='edu'>
<input type='hidden' name='issaveinfo' value=''>
</form>





这一段是含有控件的

但是我用Documenttext 获取得到了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>

<!--
<WISPAccessGatewayParam xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespace SchemaLocation="http://www.acmewisp.com/WISPAccessGatewayParam.xsd">
<Proxy>
<MessageType>110</MessageType>
<NextURL>http://cmccportal.ctclchina.com/gis.php?Hotspot_operator=CMCC&Hotspot_location=beijing&Nas_id=1099.0931.931.00&Nas_ip=218.203.208.4&User_ip=10.10.64.79&Portal_ip=211.139.92.253:7080&Client_type=wispr</NextURL>
<ResponseCode>200</ResponseCode>
<Delay>2</Delay>
</Proxy>
</WISPAccessGatewayParam>
-->

<!-- cmcccs|login_req||-->


<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Cache-Control" content="no-cache">
<title>�й��ƶ� Portal</title>


</head>


<frameset rows='1500,*' border='0' framespacing='0' frameborder=0 marginwidth='0' marginheight='0' scrolling="yes">

<frame src='https://211.139.92.253:7090/input_edu.php?wlanacip=218.203.208.4&wlanacname=1099.0931.931.00&wlanuserip=10.10.64.79&wlanacssid=&fromlocation=index' name='input' scrolling='yes' marginwidth='0' marginheight='0'>
</frame>

<noframes>
<body>
<form name="loginform" action="https://221.176.1.140/wlan/index.php" method="post">

<input type="hidden" name="wlanacname" value="1099.0931.931.00">
<input type="hidden" name="wlanuserip" value="10.10.64.79">
<input type="hidden" name="wlanacssid" value="">
<input type="hidden" name="actiontype" value="LOGIN">
<input type="hidden" name="languagetype" value="">
<input type="hidden" name="logonsessid" value="">
</form>


</body>
</noframes>
</frameset>

</html>


只有这么多,而且所有的function都没有
sonicrang 2010-09-09
  • 打赏
  • 举报
回复
谢谢8L的解释,不过我把移动无线网的源代码另存为html并从本地调用。然后使用
foreach (HtmlElement em in doc.All)
{
string str = em.Id;
if (str == "staticusernameid")
{
em.SetAttribute("value", "123"); ; break;
}
}
就可以自动填表,而且Document.Text也完整,这是为什么。难道跟编码有关系?
之前直接获取移动无线网的Document.Text不仅不完整而且有乱码
shadow841112 2010-09-09
  • 打赏
  • 举报
回复
webBrowser1.Document;简单的网页也许你能获取到,复杂的就不行了。因为你忽略了iframe等的存在。你首先要找到控件的真实所在页面的Document。
sonicrang 2010-09-09
  • 打赏
  • 举报
回复
补充一下:4L的方法我是知道的,现在遇到的问题是我获取了网页的Document
HtmlDocument doc = webBrowser1.Document;
然后
foreach (HtmlElement em in doc.All)
{
string str = em.Id;
if (str == "staticusernameid")
{
em.SetAttribute("value", "123"); ; break;
}
}
发现获取不到。我获取的是移动无线网的默认登录页面的密码和用户名框。但是换一个网页比如163的邮箱就可以获取到。莫非是这个网页加了密还是有其他原因?我用 Document.Text看了一下,移动无线网的 Document.Text少了很多东西,里面就没有控件的源代码。而163邮箱的Document.Text里面就有类似<input type = " " id = " ">的东西。

wuyq11 2010-09-08
  • 打赏
  • 举报
回复
HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
HtmlElement tbUserid = webBrowser.Document.All["username"];
HtmlElement tbPasswd = webBrowser.Document.All["password"];
tbUserid.SetAttribute("value", "");
tbPasswd.SetAttribute("value", "");
btnSubmit.InvokeMember("click");

HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument;
IHTMLElement title= (IHTMLElement)doc.getElementById("");
兔子-顾问 2010-09-08
  • 打赏
  • 举报
回复
没明白你的问题是什么。想获取什么?
sonicrang 2010-09-08
  • 打赏
  • 举报
回复
- -!没人响应
whowhen21 2010-09-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
HtmlElement tbUserid = webBrowser.Document.All["username"];
HtmlElement tbPasswd = webBrowser.Document.All["password"];
tbUser……
[/Quote]
四楼的很强大了,如果想提交表单,那就
webBrowser1.Document.Forms[0].Invoke("submit");

fgh0302 2010-09-08
  • 打赏
  • 举报
回复
每份发帖啦~~

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧