1
你好我是HtmlUnit的新手,我在編碼這個問題上有問題。根據此代碼我無法登錄我錯過了什麼?這裏是我的代碼,例如我不想登錄該網址。請檢查我正在調試很長時間的代碼。謝謝。我想單元測試它是否可以在登錄後下載excel文件,然後將其寫入流中。該網址是直播網站。請問你能幫幫我嗎。在你可以下載文件之前,你需要登錄這就是我現在的工作,但無法登錄。HtmlUnit無法自動登錄網站
String url = "https://www.frw.co.uk/login";
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
try {
HtmlPage login = webClient.getPage(url);//button.click();
final HtmlForm form = (HtmlForm) login.getElementById("loginForm");
form.getInputByName("j_username").setValueAttribute("[email protected]");
form.getInputByName("j_password").setValueAttribute("examplePassword");
HtmlPage reslogin = form.getInputByValue("Login").click();
final HtmlPage downloadPage = reslogin.getAnchorByText("Download Wine List").click();
final HtmlPage p = downloadPage.getPage();
WebResponse response2 = p.getWebResponse();
InputStream is = response2.getContentAsStream();
OutputStream outputStream = new FileOutputStream(new File(fileTarget));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
webClient.closeAllWindows();
好,我還沒有看到頭了會後悔的,這和感謝! :) –