0

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
4
  • 1
    Please show your actual code, too. If you're getting an error, show enough of the code that results in that error. Especially since the error says that you're calling something that doesn't exist: investigate that. Commented Jul 11, 2024 at 17:12
  • 1
    Suggestion: You can edit your question, and show the formatted code there, instead of in a (hard-to-read) comment. You can also add more of an explanation about what you tried, specifically, and what happened (instead of "did not work"). Commented Jul 11, 2024 at 17:21
  • I also suggest you don't keep asking for help in the comments. Be patient. This is a community of volunteers. Make your question the very best it can be (see How to Ask and minimal reproducible example) - that is typically the best way to get the best answer. Commented Jul 11, 2024 at 17:29
  • show driver initialization code... seems like it's trying to upload to grid. (remote drivers have a different way to perform uploads using setFileDetector... I believe that "se/file" command is for that) Commented Jul 11, 2024 at 20:30

2 Answers 2

0

To achieve file path copying and pasting using the Robot class in Java, you can follow these steps:

Copy the file path to the clipboard using the setClipboardData function. Use the Robot class to simulate keyboard actions for pasting the clipboard content and pressing the Enter key.

public class FilePathPaster {

public static void setClipboardData(String filePath) {
    StringSelection stringSelection = new StringSelection(filePath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}

public static void pasteFilePath() throws AWTException {
    Robot robot = new Robot();

    // Simulate Ctrl+V
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    // Simulate Enter key press
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

public static void main(String[] args) {
    try {
        // Set the file path to clipboard
        String filePath = "C:\\path\\to\\your\\file.txt";
        setClipboardData(filePath);

        // Simulate the paste action and Enter key press
        pasteFilePath();
    } catch (AWTException e) {
        e.printStackTrace();
    }
}

}

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

Comments

0
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();
        }
    }

1 Comment

Answers that contain only code are generally considered poor quality. Perhaps you could edit your answer and explain how your code resolves the problem described in your question? I think that would help others facing a similar problem.

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.