Choose a File from OpenFileDialog with C#

六眼飞鱼酱① 提交于 2020-01-06 19:29:51

问题


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?


回答1:


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");



回答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




回答3:


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);


来源:https://stackoverflow.com/questions/37524651/choose-a-file-from-openfiledialog-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!