1

My project tree

Project
 JavaRessources
 src/test/java
 myclass.java
 DeployedRessources
 webApp
 myFile.txt

in my class.java i want to upload myFile.txt with selenium webdriver:

driver.findElement(By.id("upload1")).sendKeys("myFile.txt");

when i do :

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");

it return : name of uploaded file: expected: [myFile.txt] but was []

so i dont know how to use relative path to upload this file with seleniumWebDriver

Thanks

asked Aug 7, 2014 at 13:11

2 Answers 2

1

I solved my problem. it works very well. Below is my solution if it helps someone: the code below:

// 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);
answered Aug 8, 2014 at 8:26

1 Comment

driver.findElement(By.id("upload1")).sendKeys(filePath); works!. But I used String filePath = System.getProperty("user.dir") + "/src/resources/test.pdf"; to get absolute file path of the file
0

As far i understand your question, the sendkeys you're sending only the file name. So try sending the absolute path of the file.

driver.findElement(By.id("upload1")).sendKeys("d:\\folder\\myFile.txt");

Edited according to your comment:

You need to use relative path of the file to reference it,

driver.findElement(By.id("upload1")).sendKeys("./webApp/myFile.txt");
answered Aug 7, 2014 at 14:14

12 Comments

i know that it works with your code but i want that i can acess to this file from eclipse project. because i want it can be functionnel with any machine using my code
My question looks a bit this : stackoverflow.com/questions/19640043/…
it does not work same problem name of uploaded file: expected [myFile.txt] but was []
Does your file got uploaded or no
i found an exemple here to find the url path of file but i dont know how to do it with java and selenium webdriver sqa.stackexchange.com/questions/817/…
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.