Fixing “Unknown Command” Error in Selenium When Uploading a File (Python)

Uploading files using Selenium is usually simple, but some web platforms (like Facebook Messenger) combined with newer Selenium versions can throw an error like:

bashCopyEditselenium.common.exceptions.WebDriverException: Message: unknown command: session/.../se/file

If you’re seeing this issue when trying to send files in Selenium 4+, especially using ChromeDriver, this guide will help you solve it quickly.


The Problem

Let’s start with the context. Here’s the original function causing the issue:

pythonCopyEditdef sendMedia(mediaFile):
    filePath = "file.jpg"
    try:
        fileInputBox = driver.find_element_by_xpath("//input[@type='file']")
    except:
        return False
    fileInputBox.send_keys(filePath)
    return True

And here’s the error:

bashCopyEditselenium.common.exceptions.WebDriverException: Message: unknown command: session/.../se/file

This is happening on:

  • Python 3.9
  • Selenium 4.1.0
  • Chromedriver latest
  • Trying to upload via a hidden <input type="file"> element on Facebook Messenger.

Why This Happens

In Selenium 4, especially when using W3C-compliant drivers, the internal command for uploading files (UPLOAD_FILE) no longer works reliably with remote drivers or newer versions of ChromeDriver.

The error message shows that Selenium is trying to send a deprecated internal command:
session/.../se/file – which doesn’t exist anymore.


Solution Steps (Step-by-Step)

1. Use the Correct input[type="file"] Element

Make sure the element is visible and not blocked by overlay/UI. You might need to remove “hidden” or simulate a click beforehand using JS.

pythonCopyEditfile_input = driver.find_element(By.XPATH, "//input[@type='file']")
driver.execute_script("arguments[0].style.display = 'block';", file_input)
file_input.send_keys("/absolute/path/to/file.jpg")

2. Roll Back to Compatible Selenium Version (Optional Quick Fix)

Downgrade Selenium to v3.141.0, which doesn’t rely on W3C strict protocols:

bashCopyEditpip uninstall selenium
pip install selenium==3.141.0

Downsides:

  • You lose access to newer APIs and updates.
  • Less secure and potentially deprecated long term.

3. Use Local File Detector (for remote/undetected drivers)

If you’re using remote drivers or running this in cloud/CI tools, set a file detector:

pythonCopyEditfrom selenium.webdriver.remote.file_detector import LocalFileDetector
driver.file_detector = LocalFileDetector()

Note: This only helps with remote drivers, not local ChromeDriver sessions.


4. Consider Using CDP (Chrome DevTools Protocol) for Advanced Cases

If Facebook Messenger blocks traditional file uploads, you can send files via Chrome DevTools Protocol (CDP). This is advanced, but you can simulate file inputs and drag-drop using:

pythonCopyEditfrom selenium.webdriver.chrome.webdriver import WebDriver
driver.execute_cdp_cmd("Page.setFileInputFiles", {
    "objectId": input_element_id,
    "files": ["/absolute/path/to/file.jpg"]
})

Full Working Example

# pythonCopyEditfrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from time import sleep
import os

options = Options()
driver = webdriver.Chrome(service=Service(), options=options)

driver.get("https://www.facebook.com/messages/t/YOUR_THREAD_ID")
sleep(10) # Give time for manual login if needed

try:
file_input = driver.find_element(By.XPATH, "//input[@type='file']")
driver.execute_script("arguments[0].style.display = 'block';", file_input)
abs_path = os.path.abspath("file.jpg")
file_input.send_keys(abs_path)
print("File uploaded!")
except Exception as e:
print("Upload failed:", e)

Conclusion

This is a classic case where Selenium versioning, W3C protocol changes, and complex web UIs (like Facebook Messenger) cause things to break.

✔️ Here’s what you should remember:

  • Use By.XPATH with a visible input.
  • Use driver.execute_script() to make the file input interactable.
  • Consider downgrading Selenium if the internal file command fails.
  • CDP offers more robust handling for modern JavaScript-based apps.

Previous Article

How to Handle Google Authenticator (2FA) in Selenium Automation

Next Article

Fix Selenium JavaScript Dropdown Not Working (Python)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨