这是我的 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");
有谁知道我做错了什么?