Search code examples
c#seleniumselenium-webdriveropenfiledialog

Choose a File from OpenFileDialog with C#


I have a little problem - I don't know how to Select a File and Open it in the Mozilla OpenFileDialog.

First, I open the Dialog by pressing "Browse" with Selenium and then I want to put in a File-Name (I know the exact location via Environment variable)

In my case: Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc"

So my Question, does anyone know how to handle an already open OpenFileDialog using C# - Or is it perhaps possible to handle this with Selenium?

enter image description here


Solution

  • You can use sendKeys() on the file upload element to upload a file using selenium by path. I would suggest using this instead of AutoIT or Robot.

    So instead of clicking on the browse button, you send the path directly to the file input element using sendKeys().

    Example:

    IWebElement element = driver.FindElement(By.Id("file_input"));
    element.SendKeys(
        Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc");