HtmlUnit未在页面内执行Javascript

时间:2019-01-24 22:54:30

标签: java jquery htmlunit

我一直在使用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 严重:runtimeError:消息= [指定了无效或非法的选择器(选择器:':input [form =“ loginform”]'错误:无效的选择器:*:input [form =“ loginform”])。] sourceName = [{ {3}} line = [2] lineSource = [null] lineOffset = [0]

P.S。很抱歉格式化,我对Stackoverflow格式化规则不熟悉。 P.P.S.如有任何疑问,请询问。我将不断研究这个问题。再次感谢你。

1 个答案:

答案 0 :(得分:1)

已经重写了代码以使其正常运行,希望能有所帮助。 请查看内联注释,以了解如何解决您的问题。 最后,您需要充分了解网络所基于的所有技术来进行网络自动化。希望您的学习会对此有所帮助。

    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;
                    }
                }
            }
        }
    }