如何使用 htmlunit (Web Scraping) 在页面上选择单选按钮

问题描述

我正在尝试使用 htmlunit 抓取捐赠页面。我需要填写昵称、消息、金额等输入,然后选择付款方式以打印付款的 URL。我填写文本输入没有问题,但是当我尝试选择单选按钮(付款方式)时,它不起作用。它只是选择了认按钮。

页面https://tipo.live/p/bartlomiej-skowron(波兰语)

我的代码


        WebClient webClient = new WebClient(browserVersion.CHROME);

        webClient.getoptions().setCssEnabled(false);
        webClient.getoptions().setJavaScriptEnabled(true);
        webClient.getoptions().setThrowExceptionOnScriptError(false);
        webClient.getoptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getoptions().setPrintContentOnFailingStatusCode(false);

        WebRequest request = new WebRequest(new URL("https://tipo.live/p/bartlomiej-skowron"));
        HtmlPage page = webClient.getPage(request);
        
        webClient.waitForBackgroundJavaScript(2000);


        JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
        HtmlForm form = page.getForms().get(0);

        HtmlButton button = (HtmlButton)form.getElementsByTagName("BUTTON").get(0);
        HtmlTextInput username = form.getInputByName("username");
        HtmlTextArea message = form.getTextAreaByName("message");
        HtmlTextInput amount = form.getInputByName("amount");
        HtmlInput payment = form.getInputByValue("7"); //radio button

        username.type("Nickname");
        message.type("Example Message");
        amount.type("25");

        payment.setChecked(true); // it doesnt work

        webClient.waitForBackgroundJavaScript(500);

        button.click();

        while(true){
            if (manager.getJobCount() <= 0) break;
        }
        
        HtmlPage currentPage = (HtmlPage) webClient.getCurrentwindow().getEnclosedPage();
        System.out.println(currentPage.getUrl());

我也尝试过执行 javascript,但它也不起作用:

        WebRequest request = new WebRequest(new URL("https://tipo.live/p/bartlomiej-skowron"));
        HtmlPage page = webClient.getPage(request);

        page.executeJavaScript("document.getElementById(\"7__input\").click();");
        page.executeJavaScript("document.getElementsByName(\"amount\")[0].value=\"25\";");
        page.executeJavaScript("document.getElementsByName(\"message\")[0].value=\"Example Message\";");
        page.executeJavaScript("document.getElementsByName(\"username\")[0].value=\"Nickname\";");
        page.executeJavaScript("document.getElementsByClassName(\"octf-btn octf-btn-primary octf-btn-icon\")[0].click();");

解决方法

对此做了一些调试。看起来更改付款方式(单选按钮)会触发服务器往返以告知这一点。

如果您使用 HtmlUnit 更改选择,则情况并非如此。日志显示事件被触发

echo hasFile("path",".exe")  # Find .exe files recursively at path
echo hasFile("path",".exe",recursive=false)  # Same,but no recursion

第一次猜测事件处理程序可能未注册,因为加载一个使用过的 js 库失败(在打开页面期间)

Firing Event change (Current Target: HTMLElement for HtmlRadioButtonInput[<input x-model="selected" autocomplete="off" id="7__input" class="radio-button" type="radio" name="payment_method" value="7">]);

更深入的分析需要更多时间 - 请在 GitHub 上打开针对 HtmlUnit 的修复程序,我将尝试解决此问题。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...