Logging in using HtmlUnit

Hy ... I want to log into some third-party sites using HtmlUnit. But HtmlUnit should be able to tell me if the login attempt to the input site is successful or not. Is there a way around this task using HtmlUnit. Please, help..!!!

Thanks Usman Raza

+6
source share
3 answers

I am currently using HTMLunit to enter a site with a validation and redirect page. Some of my codes for this:

//---------------------------------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 the login page, adds the username and passwords to the text fields and presses the submit button. Then we are redirected to the page โ€œWait 5 seconds or click here to go to the main pageโ€, where the โ€œContinueโ€ button is clicked. Finally, we come to our homepage, which we wanted to enter. I selected the page elements of both ID and Xpath when the identifier was not available.

+5
source

You can have HtmlUnit check the URL or find a specific element on the page, more precisely the one you know, to be present in only one case (successful entry / rejection).

+1
source

As RabidFX suggested, I suggest you check the URL, but in some cases (I have seen such situations in my experience) the URL may be the same. Then my proposal will check for a certain element, and this should be a login form, because, as a rule, an unsatisfactory login will redirect you to the same page with the same login form. This would not be a hard-coded solution, because I hope you found a way to get this login form using a common path :)

0
source

Source: https://habr.com/ru/post/887268/


All Articles