您好我是HtmlUnit的新手,我在编写这个问题时遇到了问题。根据这段代码我无法登录我错过了什么?这是我的代码,例如我不想登录该URL。请检查我的代码我这么长时间都在调试。谢谢。我希望单元测试是否可以在登录后下载excel文件。然后将其写入流。该URL是实时网站。请问你能帮帮我吗。在您下载文件之前,您需要登录我现在正在工作但无法登录的文件。
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("example@hotmail.com");
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();
答案 0 :(得分:0)
可能尝试使用cookieManager添加自动重定向
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);