pla*_*irt 12 java selenium-server selenium-webdriver
我尝试使用的远程主机上执行testsuite Selenium Standalone Server.它应该上传一个文件.我使用下面的代码来处理文件上传:
FileBrowserDialogHandler fileBrowserDialogHandler = new FileBrowserDialogHandler();
fileBrowserDialogHandler.fileUploadDialog(fileSource);
Run Code Online (Sandbox Code Playgroud)
当我远程执行它时它不起作用,因为它无法打开文件选择器窗口.输入字段在网页上如下所示:
<input type ="text"id ="file-path">
我用当前的解决方案替换了当前的解决方案,WebElement以避免图形窗口,但它不起作用.
WebElement fileInput = driver.findElement(By.id("filepathelement"));
fileInput.sendKeys(filepath);
Run Code Online (Sandbox Code Playgroud)
输入类型不是文件,因此下面的代码不起作用:
driver.findElement(By.id("myUploadElement")).sendKeys("<absolutePathToMyFile>");
Run Code Online (Sandbox Code Playgroud)
使用Java Selenium: sendKeys()或上传文件Robot Class.
此方法是将指定的文件路径设置为ClipBoard.
tell full Path of file.public static void setClipboardData(String filePath) {
StringSelection stringSelection = new StringSelection( filePath );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
Run Code Online (Sandbox Code Playgroud)
OK以选择该文件.
Go To Folder" - Command ⌘ + Shift+ G.OK打开它.enum Action {
WIN, MAC, LINUX, SEND_KEYS;
}
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 {
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;
}
Run Code Online (Sandbox Code Playgroud)
文件上传测试: -您可以fileUploadBytes.html通过单击找到文件Try it Yourself.
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();
}
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请参阅my post.
有问题的文件应该在您的程序运行的计算机(无论是本地服务器还是远程服务器)上可用,例如在您的 /resources 目录中
在您的本地计算机上,这应该可以工作。
chooseFileElement.waitForVisible().type("/file/path/filename.jpg");
clickButton("Attach File");
Run Code Online (Sandbox Code Playgroud)
然而,在远程服务器上,您需要将 LocalFileDetector 的新实例关联到该<input type=file>元素。
LocalFileDetector detector = new LocalFileDetector();
File localFile = detector.getLocalFile("/file/path/filename.jpg");
RemoteWebElement input = (RemoteWebElement) myDriver().findElement(By.id("fileUpload"));
input.setFileDetector(detector);
input.sendKeys(localFile.getAbsolutePath());
clickButton("Attach File");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
826 次 |
| 最近记录: |