【发布时间】: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