我的项目树
Project
JavaRessources
src/test/java
myclass.java
DeployedRessources
webApp
myFile.txt
在我的class.java中我想用selenium webdriver上传myFile.txt:
driver.findElement(By.id("upload1")).sendKeys("myFile.txt");
当我这样做时:
private void verifyFile(final String myId, final String src) {
final WebElement file = this.driver.findElement(By.id(myId));
final String value = file.getAttribute("value");
assertEquals("name of uploaded file: ", src, value);
}
verifyFile("upload1", "myFile.txt");
它返回:name of uploaded file: expected: [myFile.txt] but was []
所以我不知道如何使用seleniumWebDriver上传此文件的相对路径
由于
答案 0 :(得分:1)
我解决了我的问题。它运作得很好。以下是我的解决方案,如果它帮助某人: 以下代码:
// Find path url of files to upload
final String pathDir = new java.io.File("").getAbsolutePath();
final String pathFile = pathDir + "\\src\\main\\webapp\\myFile.txt";
// End find path url of files to upload
driver.findElement(By.id("upload1")).sendKeys(pathFile);
答案 1 :(得分:0)
据我所知,您的问题是发送密码,您只发送文件名。所以尝试发送文件的绝对路径。
driver.findElement(By.id("upload1")).sendKeys("d:\\folder\\myFile.txt");
根据您的评论编辑:
您需要使用文件的相对路径来引用它,
driver.findElement(By.id("upload1")).sendKeys("./webApp/myFile.txt");