Search code examples
facebookhtmlunit

HtmlUnit - Getting button from form


I am trying to log in to facebook using HtmlUnit library:

String loginURL = "https://www.facebook.com/login.php?login_attempt=1"

final WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage(loginURL);
final HtmlForm form = page1.getForms().get(0);
webClient.setJavaScriptTimeout(45000);

final HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute(login);
final HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute(password);
page1 = (HtmlPage) form.getInputByValue("Zaloguj się").click();

I have checked the button value:

<input value="Zaloguj się" tabindex="4" type="submit" id="u_0_0">

After running this code following error occurs:

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[input] attributeName=[value] attributeValue=[Zaloguj się]

Can you see here any bug? Thanks for help :)


Solution

  • I try to browse the URL mentioned "https://www.facebook.com/login.php?login_attempt=1", there is no button with value [Zaloguj się]. Possibly your computer use another default language than English.

    I suggest to get the login button by ID then do click().

    page1.getElementById("loginbutton").click();

    This is because I see the login button have ID as.

    <button value="1" class="_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy" id="loginbutton" name="login" tabindex="1" type="submit">Log In</button>