在几年前薅羊毛比较盛行的时候,账号注册机是比较热门的一款软件,那时候我也参与过很多薅羊毛的活动,也用易语言写过一些简单的账号注册机,最近在开发一款自动操作网站的应用,突然想到C#好像也可以制作这种账号注册机,就稍微试验了一下,整理一篇文章记录一下。
1.首先我们需要先创建一个Windows Forms App程序,然后在窗体中增加一个webBrowser控件,并且默认会命名为webBrowser1,这个命名可以改也可以不改,我这里没有进行更改。
2.然后我们增加一个button按钮控件,然后双击新增的button按钮进行代码编写,我这里模拟的是登录操作,注册操作同理,代码如下:
实现思路:
首先用 webBrowser1 打开网页,然后找到控件,执行对应的操作即可,下面我会把一些常用的操作进行记录,大家有需要可以收藏保存,以备不时之需。
知识记录:
1.获取非input控件的值:
webBrowser1.Document.All[“控件ID”].InnerText;
或webBrowser1.Document.GetElementById(“控件ID”).InnerText;
或webBrowser1.Document.GetElementById(“控件ID”).GetAttribute(“value”);
2.获取input控件的值:
webBrowser1.Document.All[“控件ID”].GetAttribute(“value”);;
或webBrowser1.Document.GetElementById(“控件ID”).GetAttribute(“value”);
3.给输入框赋值:
webBrowser1.Document.GetElementById(“控件ID”).SetAttribute(“value”, “控件值”);
4.CheckBox选中:
webBrowser1.Document.GetElementById(“控件ID”).SetAttribute(“Checked”, “true”);
5.根据已知有ID的元素操作没有ID的元素:
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
6.获取Div或其他元素的样式:
webBrowser1.Document.GetElementById(“addDiv”).Style;
7.直接执行页面中的脚本函数,带动态参数或不带参数都行:
Object[] objArray = new Object[1];
objArray[0] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript(“ticketbook”, objArray);
webBrowser1.Document.InvokeScript(“return false”);
8.自动点击、自动提交:
HtmlElement btnAdd = doc.GetElementById(“addDiv”).FirstChild;
btnAdd.InvokeMember(“Click”);
9.自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
this.timer1.Enabled = true;
this.timer1.Interval = 1000 * 2;
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
ClickBtn.InvokeMember(“Click”);//执行按扭操作
}
10.屏蔽脚本错误:
将WebBrowser控件ScriptErrorsSuppressed设置为True即可
11.获取网页中的Iframe,并设置Iframe的src
HtmlDocument docFrame = webBrowser1.Document.Window.Frames[“mainFrame”].Document;
或
HtmlDocument docFrame = webBrowser1.Document.All.Frames[“mainFrame”].Document;
docFrame.All[“mainFrame”].SetAttribute(“src”, “http://www.baidu.com/”);
12.网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
13.让控件聚焦
this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All[“TPL_password_1”].Focus();