【问题标题】:How to submit file in form with HtmlUnit如何使用 HtmlUnit 以表单形式提交文件
【发布时间】:2023-03-23 04:54:01
【问题描述】:

这个表格有问题

<form action="/incoming/Upload.cfg" enctype="multipart/form-data" method="post" onsubmit="return doSubmit();">
  <table id="autoWidth" border="0" style="width: 100%;">
    <tbody>
      <tr>
        <td class="h1" colspan="4" id="t_title">
          Резервная копия и Восстановление
        </td>
      </tr>
      <tr>
        <td class="blue" colspan="4"/>
      </tr>
      <tr>
        <td class="Item" id="t_backup">
          Сохранить:
        </td>
        <td colspan="3">
          <input type="button" class="buttonBigL" name="Backup" value="Резервная копия" onclick="location.href='config.bin';"/>
        </td>
      </tr>
      <tr>
        <td class="Item" id="t_file">
          Файл:
        </td>
        <td>
          <input class="text" name="filename" type="file" size="20" onkeydown="return false"/>

            
        </td>
        <td>
          <input name="Restore" type="submit" class="buttonBigL" id="Submit" value="Восстановить"/>
        </td>
        <td width="100%">
           
        </td>
      </tr>
      <tr>
        <td class="blue" colspan="4"/>
      </tr>
    </tbody>
  </table>
</form>

我想插入“备份”文件并提交表单(在浏览器中单击“恢复按钮”)。代码。

private String model = "src/test/resources/config.bin";
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setTimeout(20000);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
  webClient.setConfirmHandler(new ConfirmHandler() {
            public boolean handleConfirm(Page page, String message) {
                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1123");
                return true;
            }
        });
 final HtmlPage frame = ((HtmlPage) page.getFrameByName("frame").getEnclosedPage());

        List<HtmlForm> forms = mainFrame.getForms();

        HtmlForm htmlForm = forms.get(0);

        htmlForm.<HtmlFileInput>getInputByName("filename").setValueAttribute(model);
        htmlForm.<HtmlFileInput>getInputByName("filename").setContentType("multipart/form-data");
        HtmlPage page = (HtmlPage) mainFrame.executeJavaScript("return doSubmit()").getNewPage();

当我执行 JS 时出现错误

StrictErrorReporter:错误:消息=[无效返回] sourceName=[注入脚本] line=[1] lineSource=[return doSubmit()] lineOffset=[7]

当我使用时

htmlForm.getInputByName("Restore").click();      

什么都没发生

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    代码运行正常,过载消失。但是我在 spring 容器中运行了测试,由于某种原因我得到了这个错误。

    @RunWith(SpringRunner.class)
    @SpringBootTest
    

    如果你在没有弹簧的情况下跑步,一切正常。

    htmlForm.<HtmlFileInput>getInputByName("filename").setValueAttribute(model);
    htmlForm.<HtmlFileInput>getInputByName("filename").setContentType("multipart/form-data");
    HtmlPage restore = htmlForm.getInputByName("Restore").click(); 
    

    刷新

    问题是 - “目标服务器未能响应异常。” 我避免了这种情况

            webClient.getOptions().setTimeout(20000);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setUseInsecureSSL(true);
            webClient.getOptions().setRedirectEnabled(true);
    

    决定

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 1970-01-01
      • 2019-11-03
      • 2021-06-15
      • 1970-01-01
      • 2015-04-30
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多