Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login check using 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 ..!!!

Thanks Usman Raza

like image 473
user737865 Avatar asked May 04 '11 11:05

user737865


Video Answer


2 Answers

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");
    //----------------------------------------------------------------

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.

like image 152
TheBeeKeeper Avatar answered Oct 16 '22 19:10

TheBeeKeeper


You can have HtmlUnit check the URL, or search for a specific element on the page, more precisely one you know to be present only in one case (sucessful login / rejected).

like image 1
Silver Quettier Avatar answered Oct 16 '22 19:10

Silver Quettier