Here's a tweak for @Motine's excellent answer, because I discovered that it no longer worked after updating to selenium-webdriver 4.26. For 4.26 (and presumably later versions, though I haven't tested it!):
module Capybara::Node::Actions
alias_method :original_attach_file, :attach_file
def attach_file(*args, **kwargs)
implement_hacky_fix_for_file_uploads_with_chromedriver
original_attach_file(*args, **kwargs)
end
def implement_hacky_fix_for_file_uploads_with_chromedriver
return if @hacky_fix_implemented
Capybara.current_session
capybara_selenium_driver = session.driver
selenium_webdriver = capybara_selenium_driver.browser
bridge = selenium_webdriver.send(:bridge)
bridge.add_commands({upload_file: [:post, "session/:session_id/file"]})
@hacky_fix_implemented = true
end
end