javascript - HTML 单元由于某种原因无法找到表单。我怀疑是因为 angularJS 但不确定

标签 javascript java html angularjs htmlunit

我正在使用 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)

最佳答案

当我在禁用 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 完成更改页面源。

关于javascript - HTML 单元由于某种原因无法找到表单。我怀疑是因为 angularJS 但不确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37735391/

相关文章:

java - Android:如何恢复别名密码?

java - JUnit 测试未能测试 system.out.println 结果

html - 无法使用 CSS 调整图像大小

javascript - 开放层 3 : Interaction DragBox not working

javascript - 删除 PHP 中所有 REAL Javascript 注释

javascript - 将图像读入JS并随后对其进行压缩

jquery - 如何使用 jquery 获取 html 列表项的 id?

javascript - Reactjs检查数据更新

java - 带有 map 的 BeanUtils

html - 单击 Angular 6 中的图像时如何打开文件对话框