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)
頁面上有嵌套表單嗎?我以前也遇到過類似的問題,直到我找到了牆,直到我碰到牆... – poashoas
Ist標籤具有'id',您應該通過id屬性而不是名稱屬性。 – MrSmith42