1

I want to upload a file using selenium webdriver, the problem is that the button to upload the file has an input_type=file. I used xpath, name, ID, with sendkeys and it's not working.

Here is the code:

driver.findElement(By.xpath(".//*[@id='repondants_file']")).sendkeys("filepath");

I have also used JS:

WebElement element = driver.findElement(By.xpath(".//*[@id='repondants_file']"));
JavascriptExecutor executor = (JavascriptExecutor)getDriver();
executor.executeScript("arguments[0].click()",element );

Here is the full stacktrace:

*** Element info: {Using=xpath, value=.//*[@id='repondants_file']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:133)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:99)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:43)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:476)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at TestPackage.OperationProjet2.main(OperationProjet2.java:41)

Here is the Html source code:

a href="http://test.360-feedback-enligne.fr/professionnel-rh/app/batch/encode.php" download="liste_repondants.xlsx">
<br/>
<br/>
<label>Importez le fichier EXCEL des répondants (conforme à la trame) :</label>
<input id="repondants_file" name="repondants_file" type="file"/>

Any clues?

3
  • 1
    What do you mean by "not working"? what happens when you try this code? any exceptions? Commented Mar 19, 2017 at 17:43
  • The code is expected to upload a file but nothing happens when executing it, and I got the following error on my console: *** Element info: {Using=xpath, value=.//*[@id='repondants_file']} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) Commented Mar 19, 2017 at 17:46
  • This is not the complete stacktrace. Does it say NoSuchElementException by any chance? you should also add the relevant html to the question. Commented Mar 19, 2017 at 17:49

3 Answers 3

0

Selenium can help you to click on the particular webelement which opens the windows file selector. Selenium won't be able to help you directly to select a file from your local directory structure. In that case you have to take help of Auto IT in your Selenium/Java code to select the particular file and then through Webdriver instance you can click on upload button to upload the selected file.

Let me know if this helps you.

Sign up to request clarification or add additional context in comments.

9 Comments

I have installed AutoIT, prepared its script, and modified my code as per below: //Click repondants_file WebElement element = driver.findElement(By.id("repondants_file")); element.click(); //Call AutoIT Runtime.getRuntime().exec("C:\\Users\\za\\Desktop\\FileUpload.exe");
still getting the same stacktrace :(
well as I am still getting the same stachtrace error, it couldn't even call the AutoIT script, so I am still stuck my initial question which is Selenium not being able to find an element with input_type=file
It essentially means that the xpath you provided is not working. Can you update your question with the complete HTML DOM so that we can help you?
Use this xpath "//input[@id='repondants_file']" and let me know if the file selector box opens.
|
0

You can upload the required file using Selenium Webdriver with the help of type property of the control, as shown below:

 driver.findElement(By.xpath("//input[@type='file']")).sendkeys("filepath");

Replace filepath in above code with the actual file path. Let me know, whether it works for you.

1 Comment

IT's ok guys I have found the solution, I have used ExpectedConditions as per below ExpectedConditions.visibilityOfElementLocated(By.id("repondants_file")));
0

IT's ok guys I have found the solution, I have used ExpectedConditions as per below ExpectedConditions.visibilityOfElementLocated(By.id("reponda‌​nts_file")));

Comments

Your Answer

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.