无法使用 HtmlUnit 2.35.0 登录网站
Cannot login to a website using HtmlUnit 2.35.0
我想使用 HtmlUnit
登录 https://www.rjs.com/member/user.html#login 但失败了,我在调用登录 button.click 后仍然显示上一个页面。
如果有人能帮助我解决这个问题,我将不胜感激。
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
webClient.getOptions().setUseInsecureSSL(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setTimeout(50000);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
HtmlPage page = webClient.getPage("https://www.rjs.com/member/user.html#login");
webClient.waitForBackgroundJavaScript(10000);
System.out.println(page.asXml());
HtmlInput heUsername = (HtmlInput)page.getHtmlElementById("login_username");
HtmlPasswordInput hePassword = (HtmlPasswordInput)page.getHtmlElementById("login_pwd");
HtmlButton heLogin = (HtmlButton)(page.getFirstByXPath("//button[@class='login-btn']"));
heUsername.setValueAttribute(<my user name>);
hePassword.setValueAttribute(<my password>);
HtmlPage page2 = heLogin.click();
webClient.waitForBackgroundJavaScript(10000);
System.out.println(page2.asXml());
webClient.close();
没有凭据,我无法验证您的问题。只有一些一般提示
让 WebClient 设置尽可能简单,默认值通常很好。在我看来你只需要
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
使用 type() 填充输入字段
heUsername.type(<my user name>);
hePassword.type(<my password>);
如果您在登录后等待 js 完成,通常最好从 window 重新获取页面,因为 js 可能在登录后加载了不同的页面.
HtmlPage page2 = heLogin.click();
webClient.waitForBackgroundJavaScript(10000);
page2 = ((HtmlPage) page2.getEnclosingWindow().getEnclosedPage());
希望对您有所帮助。和往常一样,日志中可能也包含有用的提示。
这似乎在这里工作(使用最新的 2.36.0-SNAPSHOT)构建。
public static void main(String[] args) throws Exception {
String uri = "https://www.rjs.com/member/user.html#login";
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {
HtmlPage page = webClient.getPage(uri);
webClient.waitForBackgroundJavaScript(10000);
HtmlInput heUsername = (HtmlInput) page.getHtmlElementById("login_username");
HtmlPasswordInput hePassword = (HtmlPasswordInput) page.getHtmlElementById("login_pwd");
HtmlButton heLogin = (HtmlButton) page.getFirstByXPath("//button[@class='login-btn']");
heUsername.type(....);
hePassword.type(....);
heLogin.click();
webClient.waitForBackgroundJavaScript(10000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println("----------------");
System.out.println(page.asXml());
}
}
我想使用 HtmlUnit
登录 https://www.rjs.com/member/user.html#login 但失败了,我在调用登录 button.click 后仍然显示上一个页面。
如果有人能帮助我解决这个问题,我将不胜感激。
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
webClient.getOptions().setUseInsecureSSL(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setTimeout(50000);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
HtmlPage page = webClient.getPage("https://www.rjs.com/member/user.html#login");
webClient.waitForBackgroundJavaScript(10000);
System.out.println(page.asXml());
HtmlInput heUsername = (HtmlInput)page.getHtmlElementById("login_username");
HtmlPasswordInput hePassword = (HtmlPasswordInput)page.getHtmlElementById("login_pwd");
HtmlButton heLogin = (HtmlButton)(page.getFirstByXPath("//button[@class='login-btn']"));
heUsername.setValueAttribute(<my user name>);
hePassword.setValueAttribute(<my password>);
HtmlPage page2 = heLogin.click();
webClient.waitForBackgroundJavaScript(10000);
System.out.println(page2.asXml());
webClient.close();
没有凭据,我无法验证您的问题。只有一些一般提示
让 WebClient 设置尽可能简单,默认值通常很好。在我看来你只需要
webClient.getOptions().setUseInsecureSSL(true); webClient.getOptions().setThrowExceptionOnScriptError(false);
使用 type() 填充输入字段
heUsername.type(<my user name>); hePassword.type(<my password>);
如果您在登录后等待 js 完成,通常最好从 window 重新获取页面,因为 js 可能在登录后加载了不同的页面.
HtmlPage page2 = heLogin.click(); webClient.waitForBackgroundJavaScript(10000); page2 = ((HtmlPage) page2.getEnclosingWindow().getEnclosedPage());
希望对您有所帮助。和往常一样,日志中可能也包含有用的提示。
这似乎在这里工作(使用最新的 2.36.0-SNAPSHOT)构建。
public static void main(String[] args) throws Exception {
String uri = "https://www.rjs.com/member/user.html#login";
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {
HtmlPage page = webClient.getPage(uri);
webClient.waitForBackgroundJavaScript(10000);
HtmlInput heUsername = (HtmlInput) page.getHtmlElementById("login_username");
HtmlPasswordInput hePassword = (HtmlPasswordInput) page.getHtmlElementById("login_pwd");
HtmlButton heLogin = (HtmlButton) page.getFirstByXPath("//button[@class='login-btn']");
heUsername.type(....);
hePassword.type(....);
heLogin.click();
webClient.waitForBackgroundJavaScript(10000);
page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println("----------------");
System.out.println(page.asXml());
}
}