Choose a File from OpenFileDialog with C#

2019-09-12 17:12发布

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

3条回答
唯我独甜
2楼-- · 2019-09-12 17:36

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

查看更多
别忘想泡老子
3楼-- · 2019-09-12 17:39

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");
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-09-12 17:55

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);
查看更多
登录 后发表回答