简体   繁体   English

单击带有HTMLUnit的按钮仅返回部分代码

[英]Clicking on a button with HTMLUnit return just partial code

I'm trying to surf inside an asp.net site with Java using HTMLUnit. 我正在尝试使用HTMLUnit在Java的asp.net网站内进行浏览。 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? 当我在Java代码底部请求最终的println时,控制台仅返回部分代码,而不是整个网页的html ...我已经尝试添加等待JavaScript和Xpath等其他常​​见内容的方法...任何人都可以提供帮助?

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;
}

In my experience it is likely that you simply have to wait for ajax to finish loading the wanted parts of the web site. 以我的经验,您可能只需要等待ajax完成网站所需部分的加载即可。

I use methods like this: //wait for ajax no longer than this number of seconds private static final int AJAX_MAX_TRIES_SECONDS = 30; 我使用这样的方法://等待ajax的时间不超过此秒数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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM