我一直在使用 HtmlUnit 远程登录我的学校门户。据我了解,学校门户网站总是出现登录问题。我知道还有其他关于此主题的问题,但如果您不介意,请您看一下我的具体示例。
我想使用我的代码填写主页上的登录表单,然后访问我的个人资料。但是,htmlUnit 给我一个运行时错误并返回到主页。
我附上了我尝试执行的代码和我收到的错误。另外,我附上了我得到的输出(这不是我想要的)。 (这就是我最终想要获取的链接:https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage)
public class MistarLogin {
public static void main(String[] args) throws Exception {
//java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
//java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
int javaScriptLeftNum = webClient.waitForBackgroundJavaScript(3000);
// Get the first page
HtmlPage firstPage = (HtmlPage) webClient.getPage("https://mistar.oakland.k12.mi.us/novi/StudentPortal");
System.out.println(firstPage.asText());
System.out.println("\n\n\n\n\n\n\n-------------------------------------------------------------------------------\n\n\n\n\n\n");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
HtmlForm form = firstPage.getFormByName("loginform");
// Enter login and passwd
//form.getInputByName("districtid").setValueAttribute("");
form.getInputByName("Pin").setValueAttribute("email");
form.getInputByName("Password").setValueAttribute("password");
// Click "Sign In" button/link
HtmlInput loginButton = form.getInputByValue("Log In");
HtmlPage testPage = (HtmlPage) loginButton.click();
// I added the cookie section but this returns a null pointer exception
Set<Cookie> cookie = webClient.getCookieManager().getCookies();
if(cookie != null){
Iterator<Cookie> i = cookie.iterator();
while (i.hasNext()) {
webClient.getCookieManager().addCookie(i.next());
}
}
// final HtmlPage secondPage = loginButton.click();
然后打印出firstPage、testPage(输出是一样的)
<小时/>这是我得到的输出:
Student Portal
District Website
学生门户登录
用户名:
密码:
登录
0
诺维社区学区 1
正在加载信息...
正在加载信息...
<小时/>这是我得到的一个错误(最基本的错误):
com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter 运行时错误 严重:runtimeError:消息= [指定了无效或非法的选择器(选择器:':input [form =“loginform”]'错误:无效的选择器:*:input [form =“loginform”])。] sourceName = [https://mistar.oakland.k12.mi.us/novi/StudentPortal/Scripts/jquery-1.11.2.min.js?m=20150316043710] line=[2] lineSource=[null] lineOffset=[0]
附注抱歉格式化,我不熟悉 Stackoverflow 格式化规则。 附言如有任何疑问,请询问。我会不断地关注这个问题。再次感谢你。
最佳答案
已经重写了您的代码以使其正常工作,希望有所帮助。 请查看内联注释以了解如何解决您的问题。 最后,您需要充分了解网络所基于的所有技术来实现网络自动化。希望您的学习能对此有所帮助。
final String url = "https://mistar.oakland.k12.mi.us/novi/StudentPortal";
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {
HtmlPage firstPage = webClient.getPage(url);
// waitForBackgroundJavaScript has to be called after every action
webClient.waitForBackgroundJavaScript(2000);
System.out.println(firstPage.asText());
System.out.println("-------------------------------------------------------------------------------");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
HtmlForm form = firstPage.getFormByName("loginform");
// Enter login and passwd
form.getInputByName("Pin").type(XXXX);
form.getInputByName("Password").type(YYYY);
// Click "Sign In" button/link
HtmlInput loginButton = form.getInputByValue("Log In");
loginButton.click();
// wait until the js has done the job
webClient.waitForBackgroundJavaScript(20000);
// now get the current page content based on the current window; using
// HtmlPage homePage = (HtmlPage) loginButton.click();
// does not work because the page content is async replaced by javascript
HtmlPage homePage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println(homePage.asText());
System.out.println("-------------------------------------------------------------------------------");
DomElement table = homePage.getElementById("stuBannerTable");
for (DomElement tableRow : table.getElementsByTagName("tr")) {
if (tableRow.asText().contains("Dmitry Kustarnikov") && tableRow.asText().contains("Novi High School (HS)")) {
System.out.println("row found ");
for (DomElement tableCell : table.getElementsByTagName("tr")) {
if (tableCell.asText().contains("Dmitry Kustarnikov")) {
System.out.println("cell found ");
tableCell.click();
webClient.waitForBackgroundJavaScript(20000);
HtmlPage detailsPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
System.out.println(detailsPage.asText());
System.out.println("-------------------------------------------------------------------------------");
break;
}
}
}
}
}
关于java - HtmlUnit 不在页面内执行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54356568/