需要有关程序逻辑的建议

时间:2012-11-07 15:49:43

标签: c# .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 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)

Heconstantly checks.Ifapagethatexecute方法。并且因为该方法曾经完成其工作,不再有值

因此我们得到一个异常ArgumentOutRangeException。帮助如何为这种情况制作逻辑!

1 个答案:

答案 0 :(得分:1)

如果您在该方法调用中添加签入该怎么办?

var method2 = method_2("c", "...");
if (method2.Count > 0)
    method2[0].InvokeMember("click");