一、對於上傳文件, 從手動操作我們可以看出, 需要對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:/","");
}
這樣把代碼和文件上傳到服務器, 就可以找到該文件進行上傳。
這里需要注意的幾點:
- The element you want to put filepath is the "whole" input element rather than the "readonly" input element,as below, the first locator is right but the second locator will throw an exception.
- 直接調用element.sendkeys , 不需要再做一次element.clear(),否則會出現該異常:Element must be user-editable in order to clear it.
二、對於如下控件的上傳方式, 暫時無法實現: