0

I want to scrape some information in a website that requires me to login.

I've come across HtmlUnit and I'm already able to locate the login form, fill the email and passwords inputs. The problem I have is I cannot submit this form because there isn't a submit button.

I already tried creating a temporary button and it didn't work. Followed this question.

Tried locating the submit input as an Anchor, executing java script, etc, to no avail.

    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setCssEnabled(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setRedirectEnabled(true);

    // disable caching
    webClient.getCache().setMaxSize(0);

    // Get the first page
    final HtmlPage page = webClient.getPage("https://www.jpdi.pt");

    webClient.waitForBackgroundJavaScript(10000);

    final HtmlForm form = page.getHtmlElementById("login_form");

    form.getInputByName("login_email").setValueAttribute("my_email");
    HtmlInput passWordInput = form.getInputByName("login_password");

    passWordInput.setValueAttribute("my_password");

    //wait a little
    Thread.sleep(2000);

    ScriptResult result= page.executeJavaScript("document.getElementById('submit_login').click()");

    final HtmlPage secondPage  = (HtmlPage) page.getWebClient().getCurrentWindow().getEnclosedPage();

    System.out.println(secondPage.asText());

The page that is printed is the original with the two inputs filled and not the page I'm expecting.. Any ideas?

  • Can you not just find the form element and use `executeJavaScript` to execute `submit()` on it? – BretC Feb 08 '19 at 13:49
  • @BretC The javascript I used in the example above works if I use it on a real browser in the website.. Using this: `ScriptResult result= page.executeJavaScript("document.getElementById('login_form').submit();");` as you suggest returns this as a Script result: " [result=null] " as in the above case returns this: [result=net.sourceforge.htmlunit.corejs.javascript.Undefined@0] – gotItThanksBroda Feb 08 '19 at 14:04

0 Answers0