1

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

kame
  • 20,848
  • 33
  • 104
  • 159
Dominik Lemberger
  • 2,287
  • 2
  • 21
  • 33

4 Answers4

2

Selenium does not provide any native way to handle windows based pop ups. But we have some third party tools like AutoIT and RobotClass to handle those windows based pop ups. Refer those and give it a a try. AutoIT with Selenium and Java

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • I dont really want to use an external tool. Right now i have something similiar with a MozillaDialogClass that asks for a Username and Password. There it works with FindWindow and a SendMessage Commando - but somehow it doesnt work for the OpenFileDialog so i can just send keystrokes to it. – Dominik Lemberger May 30 '16 at 12:41
1

Selenium/SeleniumWebDriver does not provide any native way to handle windows based popups. Still, the best way is to miss this popup using

IWebElement element = driver.FindElement(By.Id("file_input"));
element.SendKeys(filePath);

but this is not allways is possible. And if it is not, you can use my lib:

https://github.com/ukushu/DialogCapabilities

by the following way:

OpenFileDialog:

// for English Windows
DialogsEn.OpenFileDialog(@"d:\test.txt");

//For windows with russian GUI
Dialogs.OpenFileDialog("Открыть", @"d:\test.txt");

MultiFile selection in OpenFileDialog:

var filePaths = new string[] {@"d:\test1.txt", @"d:\test2.txt", @"d:\test3.txt"};

//Or for Eng windows:
DialogsEn.OpenFileDialog(filePaths);

//for russian language OS
Dialogs.OpenFileDialog("Открыть", filePaths);
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
0

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");
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
  • Already tried this but it doesn't add the whole Filepath - it only inserts the last part: TestConfig.fpc which doesn't allow me to open the File as it is not in the starting directory of the OpenFileDialog – Dominik Lemberger May 30 '16 at 13:55
  • Then something must be wrong with your environment variable. What is the output when you print Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc"? – Mobrockers May 30 '16 at 14:00
  • The Environment Variable output works fine, as i am using it on a few different locations in the code. It just doesnt pass all of the text into this field – Dominik Lemberger May 30 '16 at 14:24
  • add: Ok i tried to take the Environment part out of the SendKeys and create a string upfront - now it works - Seems like the SendKeys somehow doesnt handle the Environment Variable right – Dominik Lemberger May 30 '16 at 15:07
  • Side-note: Uploading multiple files is also possible via SendKeys - see https://stackoverflow.com/questions/23955430/selenium-webdriver-upload-multiple-files – Joseph238 Mar 17 '21 at 22:45
0

i used selenium with Robot class from Java awt. This is my solution

public static void setClipboardData(String string) {
//StringSelection is a class that can be used for copy and paste operations.
        StringSelection stringSelection = new StringSelection(string);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

    public static void uploadFile(String fileLocation) {
        try {
//Setting clipboard with file location
            setClipboardData(fileLocation);
//native key strokes for CTRL, V and ENTER keys
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }