使用HtmlUnit进行登录检查 [英] Login check using HtmlUnit

查看:107
本文介绍了使用HtmlUnit进行登录检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...我想使用HtmlUnit登录到一些第三方网站.但是HtmlUnit应该能够告诉我输入站点的登录尝试是否成功.是否有任何方法可以使用HtmlUnit执行此任务.请帮忙.. !!!

Hy... i want to login to some 3rd party sites using HtmlUnit. But HtmlUnit should be able to tell me whether the login attempt to the input site is successful or not. Is there any way around to perform this task using HtmlUnit. Please help ..!!!

谢谢 乌斯曼·拉扎(Usman Raza)

Thanks Usman Raza

推荐答案

我目前正在使用HTMLunit登录具有变体页面并重定向的网站.我的一些代码是:

I'm currently using HTMLunit to log in to a site that has a varification page and redirect. some of my code for this is:

//---------------------------------Login Page---------------------------------

HtmlPage PageLogin = webClient.getPage(url);
HtmlElement submitButton = (HtmlElement) PageLogin.getByXPath(Xpath To Button).get(0);
HtmlTextInput name = (HtmlTextInput) PageLogin.getElementById("UserIdInput");
HtmlPasswordInput pass = (HtmlPasswordInput)PageLogin.getElementById("ADloginPasswordInput");

    name.setText(username);
    pass.setText(password);

    System.out.println("Logging in to site");
    //------------------------------------------------------------------------

   //---------------------------------Pass varified Page----------------------
    HtmlPage pagePassVarified = submitButton.click();
    System.out.println("Successfully Logged in to site");
    HtmlElement btnContinue = (HtmlElement) pagePassVarified.getElementById("BtnClickToContinue");
    //---------------------------------------------------------

    //---------------------Home Page----------------------------------
    HtmlPage pageHome = btnContinue.click();
    System.out.println("Home Page accessed");
    //----------------------------------------------------------------

此代码转到登录页面,将用户名和密码添加到文本框中,然后单击提交按钮.接下来,我们被重定向到等待5秒钟,或单击此处继续到主页"类型的页面,其中单击了继续"按钮.最后,我们到达我们要登录的主页.当没有可用的ID时,我同时通过ID和Xpath选择了页面元素.

This code goes to a login page, adds username and passwords to text boxes, and clicks the submit button. We are next redirected to a "wait 5 seconds, or click here to continue to home page" type of page, where the continue button is clicked. Lastly we arrive at our home page that we wanted to log into. I selected the page elements by both ID and Xpath when no ID was available.

这篇关于使用HtmlUnit进行登录检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆