使用 C# 从 OpenFileDialog 中选择一个文件
Choose a File from OpenFileDialog with C#
我有一个小问题 - 我不知道如何 Select 文件并在 Mozilla OpenFileDialog 中打开它。
首先,我用 Selenium 按 "Browse" 打开对话框,然后我想输入一个文件名(我通过环境变量知道确切位置)
在我的例子中:Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc"
所以我的问题是,有谁知道如何使用 C# 处理已经打开的 OpenFileDialog - 或者是否可以使用 Selenium 来处理这个问题?
Selenium 不提供任何本地方式来处理基于 windows 的弹出窗口。但我们有一些第三方工具,如 AutoIT 和 RobotClass 来处理那些基于 windows 的弹出窗口。参考那些并试一试。
AutoIT with Selenium and Java
您可以在文件上传元素上使用 sendKeys()
通过路径使用 selenium 上传文件。我建议使用它而不是 AutoIT 或 Robot。
因此,您无需单击浏览按钮,而是使用 sendKeys()
将路径直接发送到文件输入元素。
示例:
IWebElement element = driver.FindElement(By.Id("file_input"));
element.SendKeys(
Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc");
Selenium/SeleniumWebDriver 不提供任何本地方式来处理基于 windows 的弹出窗口。不过,最好的方法是使用
错过这个弹出窗口
IWebElement element = driver.FindElement(By.Id("file_input"));
element.SendKeys(filePath);
但这并不总是可能的。如果不是,您可以使用我的库:
https://github.com/ukushu/DialogCapabilities
通过以下方式:
打开文件对话框:
// for English Windows
DialogsEn.OpenFileDialog(@"d:\test.txt");
//For windows with russian GUI
Dialogs.OpenFileDialog("Открыть", @"d:\test.txt");
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);
我将 selenium 与来自 Java awt 的 Robot class 一起使用。这是我的解决方案
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();
}
}
我有一个小问题 - 我不知道如何 Select 文件并在 Mozilla OpenFileDialog 中打开它。
首先,我用 Selenium 按 "Browse" 打开对话框,然后我想输入一个文件名(我通过环境变量知道确切位置)
在我的例子中:Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc"
所以我的问题是,有谁知道如何使用 C# 处理已经打开的 OpenFileDialog - 或者是否可以使用 Selenium 来处理这个问题?
Selenium 不提供任何本地方式来处理基于 windows 的弹出窗口。但我们有一些第三方工具,如 AutoIT 和 RobotClass 来处理那些基于 windows 的弹出窗口。参考那些并试一试。 AutoIT with Selenium and Java
您可以在文件上传元素上使用 sendKeys()
通过路径使用 selenium 上传文件。我建议使用它而不是 AutoIT 或 Robot。
因此,您无需单击浏览按钮,而是使用 sendKeys()
将路径直接发送到文件输入元素。
示例:
IWebElement element = driver.FindElement(By.Id("file_input"));
element.SendKeys(
Environment.GetEnvironmentVariable("Testplatz_Config_Location") + "\TestConfig.fpc");
Selenium/SeleniumWebDriver 不提供任何本地方式来处理基于 windows 的弹出窗口。不过,最好的方法是使用
错过这个弹出窗口IWebElement element = driver.FindElement(By.Id("file_input"));
element.SendKeys(filePath);
但这并不总是可能的。如果不是,您可以使用我的库:
https://github.com/ukushu/DialogCapabilities
通过以下方式:
打开文件对话框:
// for English Windows
DialogsEn.OpenFileDialog(@"d:\test.txt");
//For windows with russian GUI
Dialogs.OpenFileDialog("Открыть", @"d:\test.txt");
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);
我将 selenium 与来自 Java awt 的 Robot class 一起使用。这是我的解决方案
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();
}
}