0

I'm trying to surf inside an asp.net site with Java using HTMLUnit. When I request the final println at the bottom of the java code the console return just partial code instead of entire webpage's html... I've already tried to add waits for the JavaScripts and other common stuff like Xpath... anyone can help?

Here's my code:

public static HtmlPage submittingForm(String user, String pssw) throws Exception {              java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);


    try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {

        webClient.getOptions().setThrowExceptionOnScriptError(false);

        final HtmlPage page1 = webClient.getPage("someUrl");

        final HtmlForm form1 = page1.getFormByName("someForm");

        final HtmlTextInput username = form1.getInputByName("inputName");

        username.setValueAttribute(user);         

        final HtmlPasswordInput password = form1.getInputByName("someName");
        password.setValueAttribute(pssw);

        final HtmlPage page2 = (HtmlPage) form1.getInputByValue(" Login ").click();

        HtmlAnchor loginLink = page2.getAnchorByHref("anyHref");
        HtmlPage page3 = loginLink.click();

        final HtmlForm form2 = page3.getFormByName("formName");

        lastPage = (HtmlPage) form2.getInputByValue("buttonName").click();

        System.out.println(lastPage.asXml());

        }

    return lastPage;
}

1 Answers1

0

In my experience it is likely that you simply have to wait for ajax to finish loading the wanted parts of the web site.

I use methods like this: //wait for ajax no longer than this number of seconds private static final int AJAX_MAX_TRIES_SECONDS = 30;

public static void waitForAjaxUntilTextAppearsInXmlSource(//
        @Nonnull final DomNode element, //
        @Nonnull final String text, //
        @Nonnull final String waitingLogMessage) throws WaitingForAjaxTimeoutException {
    LOGGER.debug("Waiting for ajax call to complete ... [" + waitingLogMessage + "]");
    final StringBuilder waitingdots = new StringBuilder("   ");
    for (int i = 0; i < AJAX_MAX_TRIES_SECONDS; i++) {
        if (element.asXml().contains(text)) {
            waitingdots.append(" ajax has finished [").append(text).append(" appeared in xml]");
            LOGGER.debug(" " + waitingdots);
            return;
        }
        waitingdots.append('.');
        wait(element);
    }
    LOGGER.warn(waitingdots.append(" ajax timeout [Text '" + text + "' did not appear in XML-source]").toString());
    LOGGER.warn("Page source:\n" + element.asXml());
    throw new WaitingForAjaxTimeoutException();
}
MrSmith42
  • 9,961
  • 6
  • 38
  • 49