HtmlPage signinPage = (HtmlPage)webClient.getPage("https://imeidb.gsma.com/imei/index");
System.out.println("signinPage -----------------------------------" + signinPage.getUrl());
HtmlForm form = signinPage.getFormByName("signIn");
HtmlTextInput loginField = (HtmlTextInput)form.getInputByName("username");
loginField.setValueAttribute(userName);
System.out.println("Username :"+loginField.toString());
HtmlPasswordInput pwdField = (HtmlPasswordInput)form.getInputByName("password");
pwdField.setValueAttribute(password);
System.out.println("Username :"+pwdField.toString());
HtmlButtonInput loginBtn = (HtmlButtonInput)signinPage.getHtmlElementById("loginImg");
System.out.println("HtmlButtonInput loginBtn -----------------------------------" + loginBtn.toString());
System.out.println("HtmlButtonInput loginBtn getHtmlElementById -----------------------------------" + signinPage.getHtmlElementById("loginImg"));
System.out.println("HtmlButtonInput signinPage getStatusCode -----------------------------------" + signinPage.getWebResponse().getStatusCode());
System.out.println("HtmlButtonInput signinPage getStatusMessage -----------------------------------" + signinPage.getWebResponse().getStatusMessage());
resultPage = (HtmlPage) loginBtn.click();
答案 0 :(得分:0)
这里已经有类似问题的答案。
没有该页面的帐户,我无法提供任何详细信息或任何可靠的解决方案。但是无论如何,有些提示:
通常,页面至少在其中进行了部分基于javascript的初始化;因为这是在检索页面或强制执行操作后等待js的好主意
HtmlPage signinPage = (HtmlPage)webClient.getPage("https://imeidb.gsma.com/imei/index");
webClient.waitForBackgroundJavaScript(2000);
使用类型方法填充条目字段
pwdField.type(password);
click()方法返回同步结果页面;在您的情况下,该按钮为无提交按钮;这通常表示网页是通过javascript(异步)刷新的。替换
resultPage = (HtmlPage) loginBtn.click();
作者
(HtmlPage) loginBtn.click();
webClient.waitForBackgroundJavaScript(20000);
resultPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
如果这没有帮助,您可以向我发送该页面的凭据,我会尽力为您提供更多详细信息。
最后-这在这里有效。结果,我进入了“帐户安全性问题”页面,就像在实际浏览器中一样。
String url = "https://imeidb.gsma.com/imei/index";
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {
HtmlPage signinPage = webClient.getPage(url);
webClient.waitForBackgroundJavaScript(10000);
// System.out.println("----------------");
// System.out.println(signinPage.asXml());
HtmlForm form = signinPage.getFormByName("signIn");
HtmlTextInput loginField = (HtmlTextInput)form.getInputByName("username");
loginField.type("xxxx");
HtmlPasswordInput pwdField = (HtmlPasswordInput)form.getInputByName("password");
pwdField.type("yyyy");
HtmlButtonInput loginBtn = (HtmlButtonInput)signinPage.getHtmlElementById("loginImg");
loginBtn.click();
webClient.waitForBackgroundJavaScript(10000);
HtmlPage resultPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println("----------------");
System.out.println(resultPage.asXml());
}