HTMLUnit HtmlTextInput 和 submitbutton

HTMLUnit HtmlTextInput and submitbutton

尝试在名为 TimeEdit 的页面上使用 HTMLUnit,该页面主要用于为学校制作日程表。我想在此处的输入字段中添加搜索词 ("DV1431"): TimeEdit

然后我想以某种方式提交。我了解到您可以使用 HTMLUnit 触发 JavaScript,还可以使用按钮甚至创建 "fake ones"。但我不确定在我的情况下什么是最好的方法。

我尝试了在 Whosebug 上找到的解决方案:Youtube-example 这个有效,但在我的 TimeEdit 页面上没有我可以使用的 HTML-form。相反,提交按钮和输入字段所在的地方只有一个名为 searchButtons 的 class。 这是我的示例代码:

    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());

当提交按钮和输入字段不在表单中时如何访问它?当我可以在输入字段中设置搜索词时如何提交?

不知道您为什么要尝试插入一个按钮以及为什么您希望该按钮可能有用。 如果您想自动化一些 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;
        }
    }

顺便说一句:此代码只是一个示例,用于证明 HtmlUnit 将能够自动化您的页面。要查找控件,您应该研究 api 提供的各种选项,并为您的用例选择合适的选项(可能是 XPath)。