我将在下面发布我的代码。我有一个带有Web浏览器组件的表单。它被命名为webBrowser1。出于某种疯狂的原因,我在调试模式下运行它,并在System.Threading.Thread.Sleep(2000)中设置了一个断点;它之前的代码似乎根本没有运行。该程序的基本思想是登录网站,搜索部件号,然后返回部件号描述。为什么会这样?有两个按钮,登录按钮和注销按钮。注销是按钮2,登录是按钮1。
public partial class Form1 : Form
{
HtmlElementCollection firstPage;
HtmlElementCollection secondPage;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// First Do login page if that comes up
try
{
// Insert userName
HtmlElementCollection firstPage = webBrowser1.Document.All;
foreach (HtmlElement element in firstPage)
{
if (element.GetAttribute("className") == "logininput" && element.Name== "username" )
{
element.SetAttribute("value","userName");
}
}
// Insert password
foreach (HtmlElement element in firstPage)
{
if (element.GetAttribute("className") == "logininput" && element.Name == "password")
{
element.SetAttribute("value", "Password");
}
}
// Submit
webBrowser1.Document.GetElementById("loginsubmit").InvokeMember("click");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
System.Threading.Thread.Sleep(2000);
//Search For Part
var mfgPartNumber = "CRCW12061R00FKEA";
webBrowser1.Document.GetElementById("txtsearch").SetAttribute("value", mfgPartNumber);
webBrowser1.Document.GetElementById("btnsearch").InvokeMember("click");
System.Threading.Thread.Sleep(50);
//Get Part
secondPage = webBrowser1.Document.All;
foreach (HtmlElement element in secondPage)
{
if (element.GetAttribute("className") == "SE-Content-PartSearch-Grid-Row-Description")
{
MessageBox.Show("got here");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
firstPage = webBrowser1.Document.All;
foreach (HtmlElement element in firstPage)
{
if (element.GetAttribute("className") == "SE-BannerLogoutLable")
{
element.InvokeMember("click");
}
}
}
}