mercredi 9 août 2017

Web Browser Control Not working properly

I will post my code below. I have a form with a web browser component. It is named webBrowser1. For some crazy reason when I run this in debug mode, and put a break point at System.Threading.Thread.Sleep(2000); The code before it appears not to have ran at all. The basic idea of this program is to login to a website , search for a part number, then return that part numbers description. Why is that happening? There are two buttons, the login button and the signout button. Signout is button 2, and login is button 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");
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire