1

I'm discovering about NHtmlUnit to build an application web. But when I try it with auto login to yahoo mail. But after I run code. I refresh the login page of Yahoo so nothing changed. Not logged.

Code:

        NHtmlUnit.WebClient driver = new NHtmlUnit.WebClient();

        driver.Options.JavaScriptEnabled = true;
        driver.Options.ThrowExceptionOnScriptError = false;
        driver.Options.ActiveXNative = true;
        driver.Options.CssEnabled = true;

        HtmlPage page = driver.GetHtmlPage("https://login.yahoo.com/config/login?");

        HtmlForm form = page.GetFormByName("login_form");
        HtmlTextInput user = (HtmlTextInput)form.GetInputByName("login");
        HtmlPasswordInput pass = (HtmlPasswordInput)form.GetInputByName("passwd");

        user.SetValueAttribute("my account");
        pass.SetValueAttribute("my pass");

        HtmlSubmitInput submitButton = (HtmlSubmitInput)page.GetElementByName(".save");

        HtmlPage nextpage = (HtmlPage)submitButton.Click();

Please help me why. I write it on .NET MVC 4 C#. Thank you very much.

2 Answers2

0

Only update below single line,due to which u may face class cast exception

HtmlButton submitButton = (HtmlButton)page.GetElementByName(".save");
Kick
  • 4,823
  • 3
  • 22
  • 29
  • Thank you for your answers, I had follow you but nothing change after I run code, I open new tab on browser and type link Mail.yahoo.com it still not logged. – user3156500 Jan 22 '14 at 03:12
  • Sry I can't understand.you mean to say that with the above code you are not able to login? – Kick Jan 22 '14 at 07:56
  • Yes brother. I want to build a tool for auto login or submit register form. And with code above a not able to login. – user3156500 Jan 28 '14 at 08:08
  • @user3156500 what issue you are facing now ? Before you was facing class cast exception – Kick Jan 28 '14 at 08:17
0

The code is completely fine, and should works.

Probably the reason you think it is not, is that you check it by a wrong method! One essential elements which makes a site (i.e. login.yahoo.com) see you as logged in user, is the cookies it provide to your browser/webdriver when you use login forms, so if you use the login procedure by an instance of NHtmlUnit.Webclient (i.e. driver) then you are only logged in inside the driver web client, not inside any other browser or client, even on the same machine.

I tried to add this as a comment, but I don't have enough reputations.

2i3r
  • 423
  • 1
  • 3
  • 13