【问题标题】:HTMLUnit HtmlTextInput and submitbuttonHTMLUnit HtmlTextInput 和提交按钮
【发布时间】:2017-01-06 03:47:36
【问题描述】:

尝试在名为 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());

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

【问题讨论】:

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


    【解决方案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;
            }
        }
    

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

    【讨论】:

    • 我读了一些关于 xPath 的文章并尝试使用它,但从未让它工作。可以找到使用它的特定按钮,但不能“单击”它,因为它在一个列表中。试图投射它,但它没有用。我不确定我的代码是否接近正确。我只是阅读了一些示例并尝试使用它。但是我尝试的每个示例在 HTML 页面上都有一个表单。然后在文本字段中输入就没有问题了,找到按钮并单击它。但是在 TimeEdit 上,他们在名为 searchButtons 的类中有一个名为“fancytypeselector”的东西。这就是现在给我造成所有问题的原因..
    • 不,它似乎不起作用。据我在测试时看到的,它在输入字段中设置了文本,但似乎没有点击。
    • 在 for 循环中我们似乎找到了按钮,但是当我尝试在 objectbasket 类中找到课程时,里面什么都没有。这就是为什么我认为 click() 实际上并没有做任何事情。如果您手动使用该页面,您将看到当您单击“Sök”时,课程将出现在左侧下方的列表中(如果课程代码有效)。对不起,如果我的解释不是最好的
    • 好吧,你说得对,我有点困惑。已经对代码进行了一些修复,现在它对我来说可以正常工作 - 希望你也可以。
    • 也许 :-),试试吧,如果失败了,我可以尝试支持你。如果您想测试该页面,可以选择查看 www.wetator.org。此工具提供了一种简单、更直观的方式来访问控件,并在后台执行了许多魔术来隐藏使用此类应用程序所需的所有魔术。
    猜你喜欢
    • 2016-11-28
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    相关资源
    最近更新 更多