Java 使用selenium webDriver从webapp上载文件
我的项目树Java 使用selenium webDriver从webapp上载文件,java,eclipse,file-upload,selenium-webdriver,Java,Eclipse,File Upload,Selenium Webdriver,我的项目树 Project JavaRessources src/test/java myclass.java DeployedRessources webApp myFile.txt 在my class.java中,我想用selenium webdriver上传myFile.txt: driver.findElement(By.id("upload1")).sendKeys("myFile.txt
Project
JavaRessources
src/test/java
myclass.java
DeployedRessources
webApp
myFile.txt
在my 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");
它返回:上传文件的名称:应为:[myFile.txt],但为[]
所以我不知道如何使用相对路径上传这个文件与seleniumWebDriver
谢谢据我所知,您发送的sendkeys仅包含文件名。因此,请尝试发送文件的绝对路径
driver.findElement(By.id("upload1")).sendKeys("d:\\folder\\myFile.txt");
根据您的评论编辑:
您需要使用文件的相对路径来引用它
driver.findElement(By.id("upload1")).sendKeys("./webApp/myFile.txt");
我解决了我的问题。它工作得很好。以下是我的解决方案,如果它对某人有帮助: 代码如下:
// 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);
我知道它可以与您的代码一起工作,但我希望能够从eclipse项目访问此文件。因为我希望它可以在任何使用我的代码的机器上运行我的问题看起来有点像这样:它不工作上传文件的问题名称:预期为[myFile.txt],但为[]你的文件上传了吗?或者我在这里找到了一个例子来查找文件的url路径,但我不知道如何使用java和selenium webdriver
driver.findelelement(By.id(“upload1”)).sendKeys(filePath)代码>工作!。但是我使用了stringfilepath=System.getProperty(“user.dir”)+“/src/resources/test.pdf”
获取文件的绝对文件路径