Unable to File Upload via sendkeys in selenium

Viewed 234

I am unable to upload the file using sendkeys in selenium . Even I tried to upload file on different website . Even I change the browser . Earlier I was using Chrome now I tried on Mozilla also . I am getting an exception . Let me share the script below

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FileUpload {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        

        System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        
        driver.get("https://html.com/input-type-file/");
        
    
        
        driver.findElement(By.xpath("//input[@id='fileupload']")).sendKeys("E:\\1.My Task\\My Task123");

    }

}


Error ----------------------

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument: File not found : E:\1.My Task\My Task123
  (Session info: chrome=92.0.4515.131)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'LAPTOP-D3V0TN3J', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_291'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 92.0.4515.131, chrome: {chromedriverVersion: 92.0.4515.43 (8c61b7e2989f2..., userDataDir: C:\Users\gunve\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:54243}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: fe1a2bfd380827509bef42daa3cee01e
    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:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
    at JavaNewLessons.FileUpload.main(FileUpload.java:25)

2 Answers

If the wepage contains any input tag with attribute type and value is file, you can directly do send_keys

So, You should replace

driver.findElement(By.xpath("//input[@id='fileupload']")).sendKeys("E:\\1.My Task\\My Task123");

with

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("E:\\1.My Task\\My Task123");

Also I would suggest to have Explicit wait before you perform this operation.

Update 1 :

Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

Since explicit waits allow you to wait for a condition to occur, they make a good fit for synchronising the state between the browser

and its DOM, and your WebDriver script.

Code :

new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='file']"))).sendKeys("E:\\1.My Task\\My Task123");

You can refer here, and see what official says click here

The error says clearly, that you are using a wrong path to the file to be uploaded.
E:\\1.My Task\\My Task123 is not a valid path to file.
Also, this path have to include the file itself.
It can not just be a path to the folder.
It should be something like:

'C:\Users\yourName\picture.png'

Also the element to send the file to can be normally located by

//input[@type='file']

XPath, so your command should be something like this:

driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\Users\yourName\picture.png");

Related