2

I'm new to C# so I looked for this topic in other questions but they weren't for me. What I am trying to do is I currently try to login to my school's servers using a c# program(Which I'm trying to implement). What I'm trying to do is I know the code of the page, so I am using web browser of c# to navigate then I just want to write name and password to the input boxes and this is where I stuck. Can you please give me any advices?

If you want to look at page: https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin

Thanks for your advices.

Here how I used the code(Edit: Added eventhandler but this is my first time using so it promts me "object reference not set to a instance of an object"):

            private void buttonGo_Click(object sender, EventArgs e)
    {
        try
        {
            string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";


            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

            webBrowser1.Navigate(input);
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement userName = doc.GetElementById("UserID");
            HtmlElement pass = doc.GetElementById("PIN");
            HtmlElement submit = doc.GetElementById("Login");

            userName.SetAttribute("value", textID.Text);
            pass.SetAttribute("value", textPASS.Text);

            submit.InvokeMember("Click");

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var webBrowser = sender as WebBrowser;
        webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
        MessageBox.Show(webBrowser.Url.ToString());
    } 
}

}

Finally I solved problem I cheated a little but managed to solve. Here is the working code:

private void buttonGo_Click(object sender, EventArgs e)
        {
            try
            {
                string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";


                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

                webBrowser1.Navigate(input);

                HtmlDocument doc = webBrowser1.Document;
                //HtmlElement userName = doc.GetElementById("UserID"); These not worked because ID of the elements were hidden so they are here to show which of these did not work.
                //HtmlElement pass = doc.GetElementById("password");
                HtmlElement submit = webBrowser1.Document.Forms[0].Document.All["PIN"].Parent.Parent.Parent.NextSibling.FirstChild;

                //userName.SetAttribute("value", textID.Text);
                //pass.SetAttribute("value", textPASS.Text);




                webBrowser1.Document.Forms[0].All["UserID"].SetAttribute("value", textID.Text);
                webBrowser1.Document.Forms[0].All["PIN"].FirstChild.SetAttribute("value", textPASS.Text);
                submit.InvokeMember("Click");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var webBrowser = sender as WebBrowser;
            webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
            MessageBox.Show(webBrowser.Url.ToString());
        } 
Kaan
  • 169
  • 2
  • 14
  • Hi, my turkish friend, if you are using xaml, just add the eventhandler of completed there... – ave Feb 15 '14 at 13:30
  • Now, I tried to add event handler but I get an error message am I using it wrong ? – Kaan Feb 15 '14 at 14:07
  • Good luck kaan, It's nice to hear that sabanci university is going to get an app :) – ave Feb 22 '14 at 16:45

1 Answers1

1

You need to find the input boxes of the username and password fields as ID's or nodes first. Then assign them as such:

HtmlDocument doc = webBrowser1.Document;
HtmlElement email = doc.GetElementById("email");
HtmlElement pass = doc.GetElementById("pass");
HtmlElement submit = doc.GetElementById("LoginButton");

email.SetAttribute("value", "InsertYourEmailHere");
//Same for password

submit.InvokeMember("Click");
DJ Olker
  • 60
  • 7
  • When I try this it throws an exception in second line that object can't something. Am I using wron ID's I look at the page with firebug and it showed me the id of user name box is UserID and for password is PIN am I doing it wrong? – Kaan Feb 14 '14 at 13:42
  • First places I would look at would be making sure that the html page is loading into the doc variable. If the HtmlDocument isn't being added to this variable then the UserID box can't be found (although I would think the exception would be thrown on the first line) – DJ Olker Feb 14 '14 at 13:56
  • Are you using a WebBrowser Control? and if so are you automating the process to navigate to this login page? If so then you would have to make sure you don't run login scripts until AFTER the page has fully downloaded and processed. WebBrowser controls have a .DocumentCompleted event that you can write an event for to make sure nothing happens out of sync with page loads. – DJ Olker Feb 14 '14 at 13:58
  • Now, I tried to add event handler but I get an error message am I using it wrong ? – Kaan Feb 15 '14 at 15:39