rubyseleniumselenium-webdriverwatir

Ruby-Selenium WebDriver 3.142.6: Unable to upload file due to "Selenium::WebDriver::Error::UnknownCommandError:"


Unable to upload the file using selenium-webdriver (3.142.6) gem in Ruby

Tech stack:

selenium-webdriver (3.142.6)

Chrome Version : 77

Chrome driver: 77

Error Message:

Selenium::WebDriver::Error::UnknownCommandError: unknown command: unknown command: session/xxxsession_idXXX/se/file
Backtrace:
       Ordinal0 [0x00FDEB13+1501971]
       Ordinal0 [0x00F5F6D1+980689]
        Ordinal0 [0x00EE765F+489055]
        Ordinal0 [0x00E9618E+156046]
        Ordinal0 [0x00E95FF4+155636]
        Ordinal0 [0x00E7220E+8718]
  Ordinal0 [0x00E72626+9766]
  Ordinal0 [0x00E72C10+11280]
 Ordinal0 [0x00F78F37+1085239]
       GetHandleVerifier [0x0107D7ED+503293]
       GetHandleVerifier [0x0107D580+502672]
       GetHandleVerifier [0x010846AC+531644]
       GetHandleVerifier [0x0107DFFA+505354]
       Ordinal0 [0x00F70606+1050118]
       Ordinal0 [0x00F7047F+1049727]
       Ordinal0 [0x00E7204B+8267]
  Ordinal0 [0x00E71D7C+7548]
  GetHandleVerifier [0x013CD83C+3976780]
      BaseThreadInitThunk [0x755738F4+36]
 RtlUnicodeStringToInteger [0x77375E13+595]
  RtlUnicodeStringToInteger [0x77375DDE+542]

Seems to be the upload is broken with this version of gem.

Code used to upload the file (watir with cheezy)

@browser.file_field(xpath: "//*[contains(text(), 'Upload')]/input[@type='file']").set(complete_path_to_file)

Solution

  • Root Cause: w3c browser capability status

    Explanation: When w3c:true selenium webdriver is throwing this error. This might be because of Selenium 3 is not at fully supporting w3c implementation.

    Solution1: Simple solution would be making w3c:false if you don't have any dependencies based of the w3c capability.

    Solution2: If solution 1 doesn't work then you have to update the selenium-webdriver-3.142.6\lib\selenium\webdriver\remote\w3c\commands.rb file. check for below line and then update it.

    upload_file: [:post, 'session/:session_id/se/file']

    Updating this line to

    upload_file: [:post, 'session/:session_id/file']

    Conclusion: Would prefer solution 1 until Selenium 4 is rolled out. If you go with solution 2 there is possibility of test failure when you try to run them on the machine where this change is not pushed/ the file might be set back with bundle install.

    You can use the method overright approach to go with solution but personally I feel it's too much.

    Posted the solution to SeleniumHQ, will submit Pull Request to SeleniumHQ once got confirmation. So that you don't face this issue in later version of Selenium Ruby WebDriver.