一、对于上传文件, 从手动操作我们可以看出, 需要对window 窗体进行操作, 而对于selenium webdriver 在这方面应用就受到了限制。 但是, 庆幸的是, 对于含有input element的上传, 我们可以直接通过sendkeys来传入文件路径,省略了对window 窗体的操作来实现文件上传, 具体实现过程如下:
1)找到上传控件element,并输入路径:
WebElement element =
driver.findElement(By.id("cloudFax-attachment-form-upload-input"));
element.sendKeys(getFilePath(text.txt));
2)路径的处理:
private String getFilePath(String resource) {
URL path =
this.getClass().getResource(resource);
return
path.toString().replaceAll("file:/","");
}
这样把代码和文件上传到服务器, 就可以找到该文件进行上传。
这里需要注意的几点:
二、对于如下控件的上传方式, 暂时无法实现:
selenium文件上传的实现,布布扣,bubuko.com
原文:http://www.cnblogs.com/jenniferhuang/p/3644453.html