javaselenium-webdriverselenium-chromedriverui-automation

Selenium Java UnsupportedCommandException unknown command: session/9845d50a442f6c23dc498210a0d253d7/se/file during file upload


I am using Selenium version: '4.12.1' and browser version 112. But facing difficulty during fileupload. I am using sendKeys method to send the filepath for xpath //input[@type='file'].

Specifications :

I'm using google chromedriver of version 112 and selenium of version 4.1

Driver : RemoteWebDriver

Code:

WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));  
String filePath = "/path/to/file.txt";  
fileInput.sendKeys(filePath);

Stacktrace:

org.openqa.selenium.UnsupportedCommandException: unknown command: unknown command: session / 9845 d50a442f6c23dc498210a0d253d7 / se / file
Build info: version: '4.12.1', revision: '8e34639b11' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.215-181.850.amzn2int.x86_64', java.version: '17.0.11' 
 Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [9845 d50a442f6c23dc498210a0d253d7, uploadFile {
            file = UEsDBBQACAgIADtor1gAAAAAAAAAAAAAAAARAAAAYnVsa1NoaXBUZXN0Lnhsc3icuwNwZdHXJZ502LFtq2PbTse2bTvp2LZt27Zt2x07mUr6+9d8M/+aX03Nq1dPdc/aZ6219z2n9rtXRgIIGAkAHBwcgNWkSgXgvz0QAQAA1AWsrRwMrRy0FVxtDO01qV0sLSYSxn730cIJeRPfoDBRw}]
             Capabilities {
                acceptInsecureCerts: true,
                browserName: chrome,
                browserVersion: 112.0 .5615 .49,
                chrome: {
                    chromedriverVersion: 112.0 .5615 .49(bd2a7bcb881c..., userDataDir: /tmp/.org.chromium.Chromium...
                    },
                    goog: chromeOptions: {
                        debuggerAddress: localhost: 46485
                    },
                    networkConnectionEnabled: false,
                    pageLoadStrategy: normal,
                    platformName: linux,
                    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: extension: minPinLength: true,
                    webauthn: extension: prf: true,
                    webauthn: virtualAuthenticators: true
                }
                Element: [
                        [RemoteWebDriver: chrome on linux(9845 d50a442f6c23dc498210a0d253d7)] - > xpath: //div[@role='dialog']//input[@accept='.xlsx']]
                         Session ID: 9845 d50a442f6c23dc498210a0d253d
                         at java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                         at java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 77)
                         at java.base / jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java: 45)
                         at java.base / java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java: 499)
                        at java.base / java.lang.reflect.Constructor.newInstance(Constructor.java: 480)
                        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(Unknown Source)
                        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(Unknown Source)
                        at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(Unknown Source)
                        at org.openqa.selenium.remote.HttpCommandExecutor.execute(Unknown Source)
                        at org.openqa.selenium.remote.RemoteWebDriver.execute(Unknown Source)
                        at org.openqa.selenium.remote.RemoteWebElement.execute(Unknown Source)
                        at org.openqa.selenium.remote.RemoteWebElement.upload(Unknown Source)
                        at java.base / java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java: 197)
                        at java.base / java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java: 1625)
                        at java.base / java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java: 509)
                        at java.base / java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java: 499)
                        at java.base / java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java: 921)
                        at java.base / java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java: 234)
                        at java.base / java.util.stream.ReferencePipeline.collect(ReferencePipeline.java: 682)
                        at org.openqa.selenium.remote.RemoteWebElement.sendKeys(Unknown Source)

What did I try :

  1. I tried using JavaScriptExecutor to make element visible but didnot work becuase the issue is with endpoint to which fileupload is sending request to
  2. Come across this post Link where it is told to override UPLOAD_FILE command did that but faced few issues

Solution

  • public void overrideFileUploadEndpoint(@NonNull RemoteWebDriver remoteWebDriver) {
            try {
                HttpCommandExecutor commandExecutor = (HttpCommandExecutor) (remoteWebDriver).getCommandExecutor();
                Field commandCodec = commandExecutor.getClass().getDeclaredField(COMMAND_CODEC);
                commandCodec.setAccessible(true);
                AbstractHttpCommandCodec w3CHttpCommandCodec = (AbstractHttpCommandCodec) commandCodec.get(commandExecutor);
                Class<?> commandSpec = Class.forName(
                        "org.openqa.selenium.remote.codec.AbstractHttpCommandCodec$CommandSpec");
                Method defineCommand = AbstractHttpCommandCodec.class.
                        getDeclaredMethod(DEFINE_COMMAND, String.class, commandSpec);
                defineCommand.setAccessible(true);
                Method post = AbstractHttpCommandCodec.class.getDeclaredMethod(POST, String.class);
                post.setAccessible(true);
                defineCommand.invoke(w3CHttpCommandCodec, UPLOAD_FILE,
                        post.invoke(w3CHttpCommandCodec, SESSION_ID + FILE));
                commandCodec.set(commandExecutor, w3CHttpCommandCodec);
            } catch (Exception e) {
                log.error("Exception encountered during overriding the endpoint path " +
                        "of upload file due to {} {}", e.getMessage(), e);
                e.printStackTrace();
            }
        }