在窗体中添加了一个webbroswer控件,代码如下,目的是实现代码的网页的自动登陆功能。在登陆www.discuz.net中最后一点点击事件不成功,不知道是不是遍历的时候找不到该控件位置,大家能否帮解决一下问题所在。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 测试平台
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.discuz.net/");
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ foreach (HtmlElement tt in webBrowser1.Document.All)
{
string str = tt.Name;
if (tt.Name == "username")
{
tt.SetAttribute("value","密码");
}
if (tt.Name == "password")
{
tt.SetAttribute("value","帐号");
}
if (tt.OuterHtml == "<button type=\"submit\" class=\"pn vm\" tabindex=\"904\" style=\"width: 75px;\"><em>登录</em></button>")
{
tt.InvokeMember("click");
}
}
} private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 测试平台
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.discuz.net/");
} private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ foreach (HtmlElement tt in webBrowser1.Document.All)
{
string str = tt.Name;
if (tt.Name == "username")
{
tt.SetAttribute("value","密码");
}
if (tt.Name == "password")
{
tt.SetAttribute("value","帐号");
}
if (tt.OuterHtml == "<button type=\"submit\" class=\"pn vm\" tabindex=\"904\" style=\"width: 75px;\"><em>登录</em></button>")
{
tt.InvokeMember("click");
}
}
} private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
<label for="ls_username">帐号</label></td>
<td><input type="text" name="username" id="ls_username" class="px vm xg1" value="UID/用户名/Email" onfocus="if(this.value == 'UID/用户名/Email'){this.value = '';this.className = 'px vm';}" onblur="if(this.value == ''){this.value = 'UID/用户名/Email';this.className = 'px vm xg1';}" tabindex="901" /></td>><label for="ls_password">密码</label></td>
<td><input type="password" name="password" id="ls_password" class="px vm" autocomplete="off" tabindex="902" /></td>
<td class="fastlg_l">很明显 帐号的 ID 是 ls_username 密码是ls_passwordwebBrowser1Document.GetElementById("").SetAttribute("value","帐号");
webBrowser1Document.GetElementById("").SetAttribute("value","密码");
webBrowser1Document.GetElementById("ls_username").SetAttribute("value","帐号");
webBrowser1Document.GetElementById("ls_password").SetAttribute("value","密码");
webBrowser1.Document.GetElementById("ls_password").SetAttribute("value","密码");
webBrowser1.Document.GetElementsByTagName("button")[0].InvokeMember("click");
另外,button是html 5的标签,低版本的用<input type="submit" value="提交" />这个网站用html 5了?
问题已经解决了,谢谢各位的帮助。QQ1009777 这位弟兄的可以做到,就是通过tag.name来查 button ,非serachbutton就是登录按钮大家可试下