プログラムがページに来てボタンを押す
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 Bot
{
public partial class FormWindow : Form
{
Random rnd;
Settings settingsForm;
Timer tm;
CallTimers callTimers;
public FormWindow()
{
rnd = new Random();
settingsForm = new Settings();
tm = new Timer();
tm.Tick += new EventHandler(tm_Tick);
wb = new WebBrowser();
InitializeComponent();
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (wb.Document.GetElementById("login-email") != null)
{
wb.Document.GetElementById("login-email").InnerText = settingsForm.tEmail.Text;
wb.Document.GetElementById("login-password").InnerText = settingsForm.tPassword.Text;
Method_1("button luxury")[0].InvokeMember("click");
}
CallMethodAfterDelay(KrysomahHunting, 5000, tm);
if (wb.Url.AbsoluteUri == "http://www.moswar.ru/metro/" && wb.ReadyState == WebBrowserReadyState.Complete)
{
wb.Stop();
method_2("c", "Выследить Крысомаху")[0].InvokeMember("click");
Method_1(new string[] { "button" })[0].InvokeMember("click");
}
}
//go to page
public void GoPage(string string_9, string string_10)
{
this.wb.Stop();
string additionalHeaders = "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
this.wb.Navigate(string_9, "", Encoding.UTF8.GetBytes(string_10), additionalHeaders);
}
public List<HtmlElement> Method_1(params string[] ClassNames)
{
List<HtmlElement> list = new List<HtmlElement>();
foreach (HtmlElement htmlElement in wb.Document.All)
{
for (int i = 0; i < ClassNames.Length; i++)
{
string b = ClassNames[i];
if (htmlElement.GetAttribute("className") == b)
{
list.Add(htmlElement);
break;
}
}
}
return list;
}
private void ButtonStart_Click(object sender, EventArgs e)
{
if (this.settingsForm.tEmail.Text == "" || this.settingsForm.tPassword.Text == "")
{
MessageBox.Show("Отсутствует логин и\\или пароль для входа на сервер МосВара.\nВвести их можно, нажав на кнопку \"Настройки\".", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Hand);
return;
}
wb.Navigate("http://www.moswar.ru");
}
private void tm_Tick(object sender, EventArgs e)
{
((MethodInvoker)tm.Tag).Invoke();
tm.Stop();
}
//go to page through time
public void CallMethodAfterDelay(MethodInvoker MethodToCall, int Delay,Timer tm)
{
tm.Interval = Delay;
tm.Tag = MethodToCall;
tm.Start();
}
public void KrysomahHunting()
{
GoPage("http://www.moswar.ru/metro/", "");
}
private List<HtmlElement> method_2(string string_9, string string_10)
{
List<HtmlElement> list = new List<HtmlElement>();
foreach (HtmlElement htmlElement in this.wb.Document.All)
{
if (htmlElement.GetAttribute("className") == string_9 && htmlElement.InnerText != null && htmlElement.InnerText.IndexOf(string_10) != -1)
{
list.Add(htmlElement);
}
}
return list;
}
}
}
論理を明確にする方法を支援する
このコードでは
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (wb.Document.GetElementById("login-email") != null)
{
wb.Document.GetElementById("login-email").InnerText = settingsForm.tEmail.Text;
wb.Document.GetElementById("login-password").InnerText = settingsForm.tPassword.Text;
Method_1("button luxury")[0].InvokeMember("click");
}
CallMethodAfterDelay(KrysomahHunting, 5000, tm);
if (wb.Url.AbsoluteUri == "http://www.moswar.ru/metro/" && wb.ReadyState == WebBrowserReadyState.Complete)
{
wb.Stop();
method_2("c", "Выследить Крысомаху")[0].InvokeMember("click");
Method_1(new string[] { "button" })[0].InvokeMember("click");
}
}
プログラムがメソッドになると
method_2("c", "Выследить Крысомаху")[0].InvokeMember("click");
例外 ArgumentOutRangeException をスローします
これが原因である可能性があると思います
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
彼は常にメソッドを実行するページをチェックします。そして、メソッドはすでにその仕事を終えているので、値はもうありません
そのため、例外 ArgumentOutRangeException が発生します。この場合のロジックの作り方を教えてください!