我正在尝试使用HTMLUnit使用Java浏览asp.net网站。当我在java代码的底部请求最终println时,控制台只返回部分代码而不是整个网页的html ...我已经尝试添加等待JavaScripts和其他常见的东西,如Xpath ...任何人都可以帮助?
这是我的代码:
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;
}
答案 0 :(得分:0)
根据我的经验,您可能只需要等待ajax完成加载网站所需的部分。
我使用这样的方法: //等待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();
}