Ahmed Kammoun
Ahmed Kammoun

Reputation: 21

Selenium unable to find an element with input_type=file

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?

Upvotes: 1

Views: 1520

Answers (3)

Ahmed Kammoun
Ahmed Kammoun

Reputation: 21

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

Upvotes: 0

Mahipal
Mahipal

Reputation: 910

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.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

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.

Upvotes: 0

Related Questions