HTMLUnit HtmlTextInput和submitbutton

时间:2017-01-04 11:14:47

标签: javascript html web-scraping htmlunit html-input

尝试在名为TimeEdit的页面上使用HTMLUnit,该页面主要用于为学校制定计划。我想在输入字段中添加一个搜索词(" DV1431"): TimeEdit

然后我想以某种方式提交。我已经读过你可以使用HTMLUnit触发JavaScript,并且你可以使用按钮甚至创建"假的"。但我不确定在我的情况下最好的方法是什么。

我尝试过在Stackoverflow上找到的解决方案:Youtube-example 这个工作,但在我的页面TimeEdit上没有我可以使用的HTML表单。相反,只有一个名为searchButtons的类,其中提交按钮和输入字段位于其中。 这是我的示例代码:

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCookieManager().setCookiesEnabled(true);

    //Get the page
    HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");

    //Just for testing purpose
    System.out.println(currentPage.getUrl());

    // Get form where submit button is located
    // But on this page there is no form, just a class called searchButtons
   HtmlForm searchForm = (HtmlForm) currentPage.getElementById("ffsearchname");

    // Get the input field.
    HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");

    // Insert the search term.
    searchInput.setText("DV1431");

    // Workaround: create a 'fake' button and add it to the form.
    HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
    submitButton.setAttribute("type", "submit");
    searchForm.appendChild(submitButton);

    // Workaround: use the reference to the button to submit the form. 
    HtmlPage newPage = submitButton.click();

    //Testing purpose
    System.out.println(newPage.getUrl());

如果提交按钮和输入字段不在表单中,如何在输入字段中设置searchterm时如何提交?

1 个答案:

答案 0 :(得分:1)

不知道为什么要尝试插入按钮以及为什么您希望该按钮可能有用。 如果你想自动化一些Html页面,你需要对html的方式有一些基本的了解,在你的情况下也需要javascript工作。

    // no need to set any options just use the default
    WebClient webClient = new WebClient(BrowserVersion.CHROME);

    // Get the page and wait for the javacode that will start with an minimal delay
    // doing something like setTimeout(init(), 10); is a common trick done by some js libs
    HtmlPage currentPage = webClient.getPage("https://se.timeedit.net/web/bth/db1/sched1/ri1Q7.html");
    currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);

    // Get the input field.
    HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("ffsearchname");

    // Insert the search term.
    searchInput.setText("DV1431");

    // the output
    DomElement output = currentPage.getElementById("objectsearchresult");
    System.out.println("- before -------------------------------------------------------------------");
    System.out.println(output.asText());
    System.out.println("----------------------------------------------------------------------------");

    // try to find the button
    for (final DomElement elem : currentPage.getElementsByTagName("input")) {
        if ("Sök".equals(((HtmlInput) elem).getValueAttribute())) {
            // click and again wait for the javascript
            currentPage = elem.click();
            currentPage.getEnclosingWindow().getJobManager().waitForJobsStartingBefore(100);

            System.out.println();
            output = currentPage.getElementById("objectsearchresult");
            System.out.println("- after --------------------------------------------------------------------");
            System.out.println(output.asText());
            System.out.println("----------------------------------------------------------------------------");
            break;
        }
    }
BTW:这段代码只是证明HtmlUnit能够自动化你的页面的一个例子。要查找控件,您应该研究api提供的各种选项,并为您的用例选择合适的选项(可能是XPath)。