How can I work with file uploads during a Webdriver test?

  • This is a canonical question, intended to provide a comprehensive answer to many related questions

    During the course of a Webdriver test, I want to upload files. However, this opens a native file picker, which Webdriver doesn't seem to support interacting with. How do I interact with it?

  • Yamikuronue

    Yamikuronue Correct answer

    6 years ago

    Uploading files using Webdriver

    Note: this answer applies to Selenium 2. Apparently Selenium 3 changed the interface again.


    If the file is on the same machine or a mapped network drive, this is actually really straightforward: You need to "type" the path to the file into the file upload control. Example:

    driver = webdriver.Firefox()
    element = driver.find_element_by_id("fileUpload")

    (Code from This SO answer)

    Using RemoteWebDriver

    In order to enable the transfer of the file from your local machine to the remote machine so it can be uploaded, you need to set what's called a File Detector. Java example:

        driver.setFileDetector(new LocalFileDetector());
        WebElement upload = driver.findElement("myfile"));

    (Code from, and more information located at, Sauce Labs)

    I am getting error `selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element`

    @Sislam You should ask a question, the comments here aren't the right place to get help. When you do, please include the snippet of HTML you're trying to automate against, as I suspect that'll be important

    @Yamikuronue is this valid for selenium 3.0.1 as well ??

    @mosaad I've never used it. Try it and let me know? I'd be glad to update if it's different now.

    @Yamikuronue driver. does not have setFileDetector for me so I guess it changed, but I don't know to what

    @mosaad My company hasn't updated to Selenium 3 yet, but I've annotated the answer and put out a bounty. Hopefully it'll get an update for Selenium 3 soon!

    Yes this is valid for selenium 3 as well, i am working with selenium 3 and it's working the same old way.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM