-3

The program comes to the page and makes pressing buttons

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;
        }

    }
}

Help how to articulate the logic

In this piece of code

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");
                }

        }

When the program comes to the method

method_2("c", "Выследить Крысомаху")[0].InvokeMember("click");

throws Exception ArgumentOutRangeException

I supposethat this couldbe caused by

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

Heconstantly checks.Ifapagethatexecute methods .And because the method once already done its job, there is no longer the values

And so we get an exception ArgumentOutRangeException. Help how to make the logic for this case!

4

1 回答 1

1

What if you add a check in on that method call?

var method2 = method_2("c", "...");
if (method2.Count > 0)
    method2[0].InvokeMember("click");
于 2012-11-07T15:53:58.333 回答