Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 由于某些原因,HTML单元找不到表单。我怀疑是因为angularJS,但不确定_Javascript_Java_Html_Angularjs_Htmlunit - Fatal编程技术网

Javascript 由于某些原因,HTML单元找不到表单。我怀疑是因为angularJS,但不确定

Javascript 由于某些原因,HTML单元找不到表单。我怀疑是因为angularJS,但不确定,javascript,java,html,angularjs,htmlunit,Javascript,Java,Html,Angularjs,Htmlunit,我使用HTMLunit登录到一个页面,然后上传一个文件。到目前为止,我已经能够登录,但我无法找到表单。它给我一个错误,它找不到元素,但它存在于HTML页面中 JAVA代码 public static void main(String[] args) { WebClient webClient = new WebClient(); webClient.getOptions().setJavaScriptEnabled(true); // webClient.getOption

我使用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">

当我在禁用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的情况下加载表单时,表单不在页面上,因此我假设它是稍后由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完成页面源代码的更改

页面上有嵌套表单吗?我以前也遇到过类似的问题,我的头撞在墙上,直到我发现,
id
中有标签,你应该按id属性而不是name属性。你在页面上有嵌套表单吗?我以前也遇到过类似的问题,我的头撞在墙上,直到我发现,
id
中有一个标签,你应该使用id属性而不是name属性。