2016-06-09 68 views
1

我正在使用HTML單元登錄到一個頁面,然後上傳一個文件。到目前爲止,我已經能夠登錄,但我無法找到表單。它給了我一個錯誤,它無法找到元素,但它存在於HTML頁面中。由於某種原因,HTML單元無法找到表單。我懷疑是因爲angularJS,但不知道

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) 
+0

頁面上有嵌套表單嗎?我以前也遇到過類似的問題,直到我找到了牆,直到我碰到牆... – poashoas

+0

Ist標籤具有'id',您應該通過id屬性而不是名稱屬性。 – MrSmith42

回答

0

的形式並不在頁面上,當我禁用了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完成更改頁面源代碼。

相關問題