0

I tried to upload a file but it throws an exception. The upload button is custom made. I even tried to click it but it gets stuck there.

    new WebDriverWait(driver,100).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='divProfileSetting']/div/div/div/div[7]/div[2]/div/div/div/label/span")));
    WebElement UploadingFile1 = driver.findElement(By.xpath("//div[@id='divProfileSetting']/div/div/div/div[7]/div[2]/div/div/div/label/span"));
            //UploadingFile1.click();

    UploadingFile1.sendKeys("E:\\Hatha.jpg");

Exception:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
  (Session info: chrome=74.0.3729.108)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'RAUNAK-MA', ip: '172.27.242.131', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628402 (536cd7adbad73a..., userDataDir: C:\Users\RAUNAK~1.MAS\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:55361}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 74.0.3729.108, webStorageEnabled: true}
Session ID: 53e0b557906a50d6f51f9aa0c2bf1a14
    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.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    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 newpackage1.flightdeckau.main(flightdeckau.java:96)

HTML:

<div class="span12 logouploadContainer">

    <input type="file" id="file" name="file" tabindex="-1" style="position: fixed; left: -9999px;">
    <div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
        <input type="text" class="input-large" disabled=""> 
        <label for="file" class="btn btn-primary">
            <i class=" icon-white icon-folder-open" data-original-title="" title=""></i> 
            <span data-original-title="" title="">Choose File</span>
        </label>
    </div>

    <div id="logo-div" class="hidden">
        <button type="button" id="btnResetLogo" class="btn" style="margin-left: 5px;" data-original-title="" title="">Remove</button>
        <div id="imgContainer" style="height: 100%; width: 100px; padding: 5px; overflow: hidden;">
            <img alt="Client Logo" id="imgClient" src="">
        </div>
    </div>


</div>

Tried with waiting for the element as well but seems like it is having some problems.

S Ahmed
  • 1,454
  • 1
  • 8
  • 14
User008
  • 377
  • 3
  • 18

2 Answers2

3

Trying to upload a file with .sendKeys() will only work when used in conjunction with an <input type=file> (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file).

Looking at your code you are trying to send the file information to a <span> element.

Instead try:

WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("file")));
uploadFileElement.sendKeys("E:\\Hatha.jpg");

This will wait for the <input type="file"> element to become visible, then it will send up the file using sendKeys(). This will not work if the <input type="file"> never becomes visible, if that is the case you can work around the problem by making it visible with JavaScript, but that would be a hack and is not representative of what an end user would do.

*EDIT*

If you do decide to go the JavaScript hack route you can do the following:

WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("file")));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.visibility='visible'", uploadFileElement);

uploadFileElement.sendKeys("E:\\Hatha.jpg");

Note that the expected condition now waits for the element to exist in the DOM rather than waiting for it to be visible, we then use JavaScript to explicitly make the element visible before using sendKeys() to interact with it.

You may not need to cast your diver object to a JavascriptExecutor. If you have an instance of RemoteWebDriver, ChromeDriver, or FirefoxDriver rather than an instance of WebDriver the method is already available.

*EDIT 2 *

Looking at this again, the real problem is that the <input type="file"> element has been pushed off the left of the screen. The fix is therefore a variation of the above. Instead of forcing the element to be visible, we can instead use JavaScript to force the offset to be 0 instead of -9999px:

WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement uploadFileElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("file")));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.left='0'", uploadFileElement);

uploadFileElement.sendKeys("E:\\Hatha.jpg");
Ardesco
  • 7,281
  • 26
  • 49
  • Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: file (tried for 15 second(s) with 100 milliseconds interval) Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.46.628402 (536cd7adbad73a..., userDataDir: C:\Users\RAUNAK~1.MAS\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, – User008 Apr 25 '19 at 07:42
  • goog:chromeOptions: {debuggerAddress: localhost:51751}, handlesAlerts: true,hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true,mobileEmulationEnabled: false, nativeEvents: true,networkConnectionEnabled:false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(),rotatable: false, setWindowRect: true, strictFileInteractability: false,takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore,unhandledPromptBehavior: ignore, version: 74.0.3729.108, webStorageEnabled: true} – User008 Apr 25 '19 at 07:43
  • Does the input element ever become visible? If not the above will fail and you will need to do something to make it visible (hence the last paragraph). I'll update to add some code to do that, there are obvious cadets to doing this though. – Ardesco Apr 25 '19 at 13:08
1

ElementNotInteractableException is caused when an element is found, but you can not interact with it. For instance, you may not be able to click or send keys.

There could be several reasons for this:

  1. The element is not visible / not displayed.
  2. The element is off screen.
  3. The element is behind another element or hidden.
  4. Some other action needs to be performed by the user first to enable it.

Solution

Wait until an element is visible / clickable

I see you already added wait but it is configured to only wait until 100ms to timeout. So if element is not interactable in 100ms wait will end. Try increasing it atleast to 1 second i.e 1000ms or it depends on speed of site.

Mustahsan
  • 3,852
  • 1
  • 18
  • 34
  • tried with more wait time but it is still the same @Mustahsan – User008 Apr 25 '19 at 06:46
  • `WebDriverWait()` parameter time is in seconds as the parameter's name suggests `timeOutInSeconds`. OP is already waiting for 100 seconds. It is highly unlikely that more waiting will solve the problem. – S Ahmed Apr 25 '19 at 07:26