如何从另一个类文件中的函数使用webbrowser?

时间:2012-05-20 00:27:15

标签: c# browser

这是我的Program.cs

    private void button2_Click(object sender, EventArgs e)
    {
        InputSimulator IS = new InputSimulator();
        IS.SimulateTextInput("login", "username");
        IS.SimulateTextInput("lpass", "password");
        IS.SimulateButtonClick("login");
    }

这是我的InputSimulator.cs

namespace Functions
{
    class InputSimulator
    {
        public void SimulateTextInput(string attName, string attValue)
        {
            Form1 mainForm = new Form1();
            HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

            foreach (HtmlElement element in col)
            {
                if (element.GetAttribute("name").Equals(attName))
                {
                element.SetAttribute("value", attValue);
                }
            }
        }

        public void SimulateButtonClick(string attName)
        {
            Form1 mainForm = new Form1();
            HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

            foreach (HtmlElement element in col)
            {
                if (element.GetAttribute("value").Equals(attName))
                {
                    element.InvokeMember("click");
                }
            }
        }
    }
}

由于某种原因,它给了我一个NullReferenceException,我似乎无法弄清楚我做错了什么,因为它没有给我任何直接的错误。它出错的地方是当我点击按钮然后在以下行:

HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在创建隐藏(不可见)的新表单实例,并且未初始化webBrowser1,因此Document为null。如果您想使用现有的Form1实例,您可以将其作为对此函数的引用传递或添加到构造函数。

例如,将其作为参考传递:

public void SimulateTextInput(Form1 mainForm, string attName, string attValue)
{
   //Form1 mainForm = new Form1();
   if(mainForm == null)
        throw new ArgumentNullException("mainForm");
   HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

   foreach (HtmlElement element in col)
   {
      if (element.GetAttribute("name").Equals(attName))
      {
         element.SetAttribute("value", attValue);
      }
   }

}

添加到构造函数:

class InputSimulator
{
    Form1 mainForm = null;

    public InputSimulator(Form1 inputForm)
    {
        if (inputForm == null)
            throw new ArgumentNullException("inputForm");

        mainForm = inputForm;
    }

    public void SimulateTextInput(string attName, string attValue)
    {
        HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement element in col)
        {
            if (element.GetAttribute("name").Equals(attName))
            {
                element.SetAttribute("value", attValue);
            }
        }
    }

    public void SimulateButtonClick(string attName)
    {
        HtmlElementCollection col = mainForm.webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement element in col)
        {
            if (element.GetAttribute("value").Equals(attName))
            {
                element.InvokeMember("click");
            }
        }
    }
}