Java Moodle上的HtmlUnit:FileUpload失败,尽管有成功消息
我尝试将文件上载到moodle2容器。这是通过yui完成的。为了跳过这些内容,我在WebClient(浏览器)中禁用java脚本以获取“noscript”元素。在分析了这些之后,我进入了一个页面,其中moodle的名字被称为 页面看起来像这样Java Moodle上的HtmlUnit:FileUpload失败,尽管有成功消息,java,file-upload,moodle,htmlunit,Java,File Upload,Moodle,Htmlunit,我尝试将文件上载到moodle2容器。这是通过yui完成的。为了跳过这些内容,我在WebClient(浏览器)中禁用java脚本以获取“noscript”元素。在分析了这些之后,我进入了一个页面,其中moodle的名字被称为 页面看起来像这样 <form action="http://demo.moodle.net/repository/filepicker.php?ctx_id=41&itemid=861552155&env=editor&
<form action="http://demo.moodle.net/repository/filepicker.php?ctx_id=41&itemid=861552155&env=editor&course=2&maxbytes=67108864&areamaxbytes=-1&maxfiles=-1&subdirs=1&sesskey=chtRoKYBlC&action=browse&draftpath=%2F&savepath=%2F&repo_id=3" method="post" enctype="multipart/form-data" style="display:inline">
<label>Attachment: </label><input name="repo_upload_file" type="file"><br>
<input name="action" value="upload" type="hidden"><br>
<input name="draftpath" value="/" type="hidden"><br>
<input name="savepath" value="/" type="hidden"><br>
<input name="repo_id" value="3" type="hidden"><br>
<input value="Upload this file" type="submit">
</form>
附件:
针对demo.moodle.org的完整案例(首先在moodle课程“我的第一门课程”id=2中手动创建名为“Test”的文件夹):
import com.gargoylesoftware.htmlunit.WebClient;
导入com.gargoylesoftware.htmlunit.html.HtmlAnchor;
导入com.gargoylesoftware.htmlunit.html.HtmlElement;
导入com.gargoylesoftware.htmlunit.html.HtmlFileInput;
导入com.gargoylesoftware.htmlunit.html.HtmlForm;
导入com.gargoylesoftware.htmlunit.html.HtmlPage;
导入com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
导入com.gargoylesoftware.htmlunit.html.HtmlSpan;
导入com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
导入com.gargoylesoftware.htmlunit.html.HtmlTextInput;
导入java.util.List;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类MoodleUpload{
/**WebClient实例*/
私有静态最终WebClient浏览器=新WebClient();
/**当前HTMLPage*/
私有HtmlPage当前页面;
公共MoodleUpload(){
//初始化网络客户端
browser.getOptions().setJavaScriptEnabled(true);
browser.getOptions().setRedirectEnabled(true);
browser.getOptions().setThroweExceptionOnScriptError(false);
browser.getOptions().setCssEnabled(false);
browser.getOptions().setUseInsecureSSL(true);
browser.getOptions().setThroweExceptionOnFailingStatusCode(false);
browser.getCookieManager().setCookiesEnabled(true);
试一试{
登录();
currentPage=(HtmlPage)browser.getPage(“http://demo.moodle.net/course/view.php?id=2");
修改文件夹(getFolderByName(“测试”);
}捕获(例外e){
Logger.getLogger(MoodleUpload.class.getName()).log(Level.SEVERE,null,e);
}
}
//在moodle上执行登录
private void login()引发异常{
currentPage=(HtmlPage)browser.getPage(“http://demo.moodle.net/");
//获取登录表单,填写必填字段并执行单击
((HtmlTextInput)currentPage.getElementById(“登录\用户名”)).setValueAttribute(“管理器”);
((HtmlPasswordInput)currentPage.getElementById(“登录密码”)).setText(“沙盒”);
列出投入;
inputs=(List)currentPage.getByXPath(“//form/div/input[@type='submit']”);
获取(0)。单击();
}
//查找指向具有给定名称的文件夹的链接
私有字符串getFolderByName(字符串名称)引发异常{
列表跨度;
span=(列表)currentPage.getByXPath(//a/span[@class='instancename'和text()=''+name+']);
System.out.println(((HtmlAnchor)span.get(0.getenclosuringelement(“a”)).getHrefAttribute());
return((HtmlAnchor)span.get(0.getEnclosuringElement(“a”)).getHrefAttribute();
}
//尝试将文件上载到moodle文件夹
私有void modifyFolder(字符串路径)引发异常{
//禁用javascript以获取noscript元素
browser.getOptions().setJavaScriptEnabled(false);
currentPage=(HtmlPage)browser.getPage(路径);
//单击“编辑设置”
列出锚;
锚=(列表)currentPage.getByXPath(//ul/li/p[@class='tree\u item leaf']]/a[text()='Edit settings']);
currentPage=(HtmlPage)browser.getPage(anchors.get(0.getHrefAttribute());
//获取文件上传程序
列出;
up=(List)currentPage.getByXPath(“//noscript/div/object”);
currentPage=(HtmlPage)browser.getPage(up.get(0.getAttribute(“数据”));
//获取“添加文件”-页面
列出要素;
elements=(List)currentPage.getByXPath(“//div[@class='filemanager-toolbar']”);
currentPage=(HtmlPage)browser.getPage(((HtmlLanchor)elements.get(0.getFirstChild()).getHrefAttribute());
elements=(List)currentPage.getByXPath(“//a[text()='Upload a file']”);
currentPage=(HtmlPage)browser.getPage(((HtmlAnchor)elements.get(0)).getHrefAttribute();
//获取文件上传表单
元素=(列表)currentPage.getByXPath(“//表单”);
HtmlFormUploadForm=(HtmlForm)元素。获取(0);
HtmlFileInput fileInput=uploadForm.getInputByName(“repo_upload_file”);
fileInput.setValueAttribute(“file:///F:/Test.xlsx");
setContentType(“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”);
//提交
列出投入;
inputs=(List)currentPage.getByXPath(“//form/input[@type='submit']”);
currentPage=输入。获取(0)。单击();
System.out.println(currentPage.asText());
}
公共静态void main(字符串[]args){
MoodleUpload tl=新的MoodleUpload();
}
}
如果我查看当前页面,我会从moodle收到一条成功消息。但该文件不在目标容器中。我尝试了对我的代码和对应于filepicker.php的“documentation”的表单的action参数进行几次修改,但都没有成功。如果我在Firefox中填写表格,一切正常。如果我跳过setValue调用,我还会收到一条失败消息。所以似乎发生了一些事情
可能这与其他隐藏字段有关?感谢您的帮助。如果这是一个可行的解决方案,我还将使用javascript和yui功能。奇怪的是,需要禁用javascript才能上载文件 无论如何,使用真正的Chrome:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlFileInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MoodleUpload {
/** Instance of WebClient */
private static final WebClient browser = new WebClient();
/** Current HTMLPage */
private HtmlPage currentPage;
public MoodleUpload(){
//intialize WebClient
browser.getOptions().setJavaScriptEnabled(true);
browser.getOptions().setRedirectEnabled(true);
browser.getOptions().setThrowExceptionOnScriptError(false);
browser.getOptions().setCssEnabled(false);
browser.getOptions().setUseInsecureSSL(true);
browser.getOptions().setThrowExceptionOnFailingStatusCode(false);
browser.getCookieManager().setCookiesEnabled(true);
try{
login();
currentPage = (HtmlPage) browser.getPage("http://demo.moodle.net/course/view.php?id=2");
modifyFolder(getFolderByName("Test"));
} catch (Exception e){
Logger.getLogger(MoodleUpload.class.getName()).log(Level.SEVERE, null, e);
}
}
//perform login on moodle
private void login() throws Exception{
currentPage = (HtmlPage) browser.getPage("http://demo.moodle.net/");
//get login-Form, fill in required fields and perform click
((HtmlTextInput) currentPage.getElementById("login_username")).setValueAttribute("manager");
((HtmlPasswordInput) currentPage.getElementById("login_password")).setText("sandbox");
List<HtmlSubmitInput> inputs;
inputs = (List) currentPage.getByXPath("//form/div/input[@type='submit']");
inputs.get(0).click();
}
//finds link to folder with given name
private String getFolderByName (String name) throws Exception{
List<HtmlSpan> spans;
spans = (List) currentPage.getByXPath("//a/span[@class='instancename' and text()='"+name+"']");
System.out.println(((HtmlAnchor) spans.get(0).getEnclosingElement("a")).getHrefAttribute());
return ((HtmlAnchor) spans.get(0).getEnclosingElement("a")).getHrefAttribute();
}
//trys to upload a file to a moodle folder
private void modifyFolder(String path) throws Exception{
//disable javascript to get noscript elements
browser.getOptions().setJavaScriptEnabled(false);
currentPage = (HtmlPage) browser.getPage(path);
//click 'edit settings'
List<HtmlAnchor> anchors;
anchors = (List) currentPage.getByXPath("//ul/li/p[@class='tree_item leaf']/a[text()='Edit settings']");
currentPage = (HtmlPage) browser.getPage(anchors.get(0).getHrefAttribute());
//get file uploader
List<HtmlElement> up;
up = (List) currentPage.getByXPath("//noscript/div/object");
currentPage = (HtmlPage) browser.getPage(up.get(0).getAttribute("data"));
//get "add file"-Page
List<HtmlElement> elements;
elements = (List) currentPage.getByXPath("//div[@class='filemanager-toolbar']");
currentPage = (HtmlPage) browser.getPage(((HtmlAnchor) elements.get(0).getFirstChild()).getHrefAttribute());
elements = (List) currentPage.getByXPath("//a[text()='Upload a file']");
currentPage = (HtmlPage) browser.getPage(((HtmlAnchor) elements.get(0)).getHrefAttribute());
//get file upload form
elements = (List) currentPage.getByXPath("//form");
HtmlForm uploadForm = (HtmlForm) elements.get(0);
HtmlFileInput fileInput = uploadForm.getInputByName("repo_upload_file");
fileInput.setValueAttribute("file:///F:/Test.xlsx");
fileInput.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//submit
List<HtmlSubmitInput> inputs;
inputs = (List) currentPage.getByXPath("//form/input[@type='submit']");
currentPage = inputs.get(0).click();
System.out.println(currentPage.asText());
}
public static void main(String[] args){
MoodleUpload tl = new MoodleUpload();
}
}
currentPage = anchors.get(0).click();
currentPage = (HtmlPage) browser.getPage(anchors.get(0).getHrefAttribute());