通过使用RemoteWebElement 类,您可以使用以下代码上传文件。
// TEST URL: "https://www.filehosting.org/"
// LOCATOR: "//input[@name='upload_file'][@type='file'][1]"
LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile( filePath );
RemoteWebElement input = (RemoteWebElement) driver.findElement(By.xpath( locator ));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
input.click();
使用 Java Selenium: sendKeys() 或 Robot Class 上传文件。
此方法是将指定的文件路径设置为剪贴板。
- 将数据复制到剪贴板。
public static void setClipboardData(String filePath) {
StringSelection stringSelection = new StringSelection( filePath );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
- 在 Finder 窗口中找到文件并按
OK 选择文件。
- WIN [ Ctrl + V ]i>
- MAC
- "
Go To Folder" - Command ⌘ + Shift + G.
- 粘贴 - Command ⌘ + V 和
- 按
OK打开它。
enum Action {
WIN, MAC, LINUX, SEND_KEYS, FILE_DETECTOR;
}
public static boolean FileUpload(String locator, String filePath, Action type) {
WebDriverWait explicitWait = new WebDriverWait(driver, 10);
WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( By.xpath(locator) ));
if( type == Action.SEND_KEYS ) {
element.sendKeys( filePath );
return true;
} else if ( type == ActionType.FILE_DETECTOR ) {
LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile( filePath );
RemoteWebElement input = (RemoteWebElement) driver.findElement(By.xpath(locator));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
input.click();
return true;
} else {
try {
element.click();
Thread.sleep( 1000 * 5 );
setClipboardData(filePath);
Robot robot = new Robot();
if( type == Action.MAC ) { // Apple's Unix-based operating system.
// “Go To Folder” on Mac - Hit Command+Shift+G on a Finder window.
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_META);
// Paste the clipBoard content - Command ⌘ + V.
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
// Press Enter (GO - To bring up the file.)
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
return true;
} else if ( type == Action.WIN || type == Action.LINUX ) { // Ctrl + V to paste the content.
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
robot.delay( 1000 * 4 );
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
return true;
} catch (AWTException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
文件上传测试:-点击Try it Yourself可以找到fileUploadBytes.html文件。
public static void uploadTest( RemoteWebDriver driver ) throws Exception {
//driver.setFileDetector(new LocalFileDetector());
String baseUrl = "file:///D:/fileUploadBytes.html";
driver.get( baseUrl );
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
FileUpload("//input[1]", "D:\\log.txt", Action.SEND_KEYS);
Thread.sleep( 1000 * 10 );
FileUpload("//input[1]", "D:\\DB_SQL.txt", Action.WIN);
Thread.sleep( 1000 * 10 );
driver.quit();
}
使用 Selenium:sendKeys() 当您想从本地计算机传输文件(参考本地文件)到 Grid-Node 服务器时,您需要使用 setFileDetector 方法。通过使用这个 Selenium-Client 将通过 JSON Wire 协议传输文件。
欲了解更多信息,请参阅saucelabs fileUpload Example
driver.setFileDetector(new LocalFileDetector());