【问题标题】:HTML unit cannot find form for some reason. I suspect because of the angularJS but not sureHTML 单元由于某种原因找不到表单。我怀疑是因为 angularJS 但不确定
【发布时间】:2016-10-10 15:55:02
【问题描述】:

我正在使用 HTMLunit 登录页面然后上传文件。到目前为止,我已经能够登录,但我无法找到表单。它给了我一个错误,它找不到元素,但它存在于 HTML 页面中。

JAVA 代码。

public static void main(String[] args) {
    WebClient webClient = new WebClient();
    webClient.getOptions().setJavaScriptEnabled(true);
   // webClient.getOptions().set(true);

    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    try {
        HtmlPage page = (HtmlPage) webClient
                .getPage("https://controlcenter-itv2.centurylink.com/business/controlcenter/home");  //loggin into this page
        HtmlForm form = page.getFormByName("lqLogin");        //grabbing the form on the page
        ((HtmlInput) form.getFirstByXPath("//*[@id='userId']")).setValueAttribute("*****");      //setting up the username for the field
        HtmlInput passWordInput = form.getInputByName("password");   
        passWordInput.removeAttribute("disabled");
        passWordInput.setValueAttribute("****");  // password for the field

       HtmlPage page1 = form.getInputByValue("Login").click(); // works fine
       HtmlPage page2= (HtmlPage) webClient.getPage("https://controlcenter-itv2.centurylink.com/business/controlcenter/ordering/initiate-orders-whsl/batch");
        System.out.println(page2.asText());


        HtmlForm form1 = page2.getFormByName("orderForm"); //doesnt work
                        //page2.getFirstByXPath("//*[@id='batchUpload']");  

       // page1 = form1.getInputByValue("Browse").click(); 




      //*[@id="fileName"]



        HtmlFileInput input = (HtmlFileInput)form1.getFirstByXPath("//*[@id='fileName']");  //trying to get the form on the upload page not working

       // input.setContentType("text/html");

           //file that needs to be uploaded
        //input.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        input.setValueAttribute("file:/C:\fileuploadTEST.txt");

这是 HTML 代码

<form id="batchOrderForm" class="ng-pristine ng-valid" name="orderForm">

这是我遇到的错误

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[orderForm]
    at com.gargoyles[enter image description here][1]oftware.htmlunit.html.HtmlPage.getFormByName(HtmlPage.java:644)

【问题讨论】:

  • 页面上有嵌套表单吗?我之前也遇到过类似的问题,一直用头撞墙,直到我发现......
  • 如果标签在id 中,您应该使用id-attribute 而不是name-attribute。

标签: javascript java html angularjs htmlunit


【解决方案1】:

当我在禁用 javascript 的情况下加载表单时,该表单不在页面上,因此我假设它是稍后由 javascript 添加的。

所以您的问题可能是您必须等待表单出现。

一般这样搜索问题:

如果例如找不到表单,我只是在尝试访问元素之前记录页面源。

在你的情况下:

 HtmlPage page2= (HtmlPage) webClient.getPage("https://controlcenter-itv2.centurylink.com/business/controlcenter/ordering/initiate-orders-whsl/batch");
        System.out.println(page2.asXml());


        HtmlForm form1 =(HtmlForm) page2.getElementById("batchOrderForm");

如果可以在输出中找到所需的元素,我会查看日志输出。通常我需要等待一些 javascrit 完成更改页面源。

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多