简体   繁体   English

NHtmlUnit无法提交按钮

[英]NHtmlUnit cannot submit button

I'm discovering about NHtmlUnit to build an application web. 我发现有关NHtmlUnit来构建应用程序网站。 But when I try it with auto login to yahoo mail. 但是,当我尝试使用自动登录到yahoo邮件进行尝试时。 But after I run code. 但是在我运行代码之后。 I refresh the login page of Yahoo so nothing changed. 我刷新了Yahoo的登录页面,所以什么都没有改变。 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#. 我在.NET MVC 4 C#上编写它。 Thank you very much. 非常感谢你。

仅在单行下更新,因为您可能会遇到类强制转换异常

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

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 (ie 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 (ie driver ) then you are only logged in inside the driver web client, not inside any other browser or client, even on the same machine. 使网站(即login.yahoo.com)将您视为已登录用户的一个基本要素是,当您使用登录表单时,它向浏览器/ webdriver提供的cookie,因此,如果您通过NHtmlUnit.Webclient实例使用登录过程, NHtmlUnit.Webclient (即driver ),那么您仅在driver Web客户端内部登录,而不在任何其他浏览器或客户端内部登录,即使在同一台计算机上也是如此。

I tried to add this as a comment, but I don't have enough reputations. 我试图添加此内容作为评论,但我的声誉不高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM