C# Selenium WebDriver 和浏览器选择文件对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8851051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium WebDriver and browsers select file dialog
提问by Oleg Strokatyy
I'm using selenium webdriver, C#.
我正在使用 selenium webdriver,C#。
Is it possible to make work webdriver with Firefox select file dialog? Or must I use something like AutoIt?
是否可以使用 Firefox 选择文件对话框制作工作 webdriver?还是我必须使用 AutoIt 之类的东西?
采纳答案by prestomanifesto
If you are trying to select a file for upload Selenium 2 supports HTML file inputs. For example:
如果您尝试选择要上传的文件,Selenium 2 支持 HTML 文件输入。例如:
HTML
HTML
<input type="file" id="uploadhere" />
Selenium Code
硒代码
IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\Some_Folder\MyFile.txt");
Basically you "type" (with SendKeys) the full file path to the file input element. Selenium handles the file selection dialog for you.
基本上,您“键入”(使用SendKeys)文件输入元素的完整文件路径。Selenium 为您处理文件选择对话框。
However if you want to manipulate an arbitrary file selection dialog, then like Anders said, you have to go outside of Selenium.
但是,如果您想操作任意文件选择对话框,那么就像 Anders 所说的那样,您必须离开 Selenium。
回答by Anders
No, WebDriver cannot interact with dialogs - this is because dialogs are the domain of the operating system and not the webpage.
不,WebDriver 不能与对话框交互 - 这是因为对话框是操作系统的域,而不是网页。
I know people that have had luck with autoit as well as the Automation API provided by .Net.
我知道有人对 autoit 以及 .Net 提供的自动化 API 感到很幸运。
Another option would be to skip the file dialog entirely and issue a POST or a GET, but this requires more advanced knowledge of the website as well as understanding how construct a POST/GET.
另一种选择是完全跳过文件对话框并发出 POST 或 GET,但这需要更高级的网站知识以及了解如何构建 POST/GET。
You could try Webinator, it is similar to Selenium in the sense that it is powered by WebDriver. It provides file dialog capabilities and I've had great success with it.
您可以尝试Webinator,它类似于 Selenium,因为它由 WebDriver 提供支持。它提供了文件对话框功能,我在它方面取得了巨大的成功。
回答by Bijoy Meethal
Here is another solution using remotewebdriver, it works like magic and I loved it.
这是使用 remotewebdriver 的另一种解决方案,它的作用就像魔术一样,我喜欢它。
Here is the class I have:
这是我的课程:
driver.findElementByLinkText("Upload Files").click();
driver.setLogLevel(Level.ALL);
System.out.println(driver.getCurrentUrl());
WebElement element = driver.findElement(By.xpath("//input[@name='file_1']"));
LocalFileDetector detector = new LocalFileDetector();
//Now, give the file path and see the magic :)
String path = "D://test66T.txt";
File f = detector.getLocalFile(path);
((RemoteWebElement)element).setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());
//now click the button to finish
driver.findElementByXPath("//html/body/div[9]/div[1]/a/span").click();
回答by sparkyShorts
If you want to upload a file, and not use the WebDriver, the only solution I've come across is AutoIt. It allows you to write a script and convert it to an executable which you can then call from within your code. I've used it successfully while working with an ActiveX control.
如果你想上传文件,而不是使用 WebDriver,我遇到的唯一解决方案是 AutoIt。它允许您编写脚本并将其转换为可执行文件,然后您可以在代码中调用该脚本。我在使用 ActiveX 控件时成功地使用了它。
回答by Bart Wojtala
Another approach is to use System.Windows.Forms.SendKeys.SendWait("pathToFile").
I use it with success everywhere where i cant just send keys to element like described by @prestomanifesto.
另一种方法是使用System.Windows.Forms.SendKeys.SendWait("pathToFile").
我在任何地方都成功地使用它,我不能像@prestomanifesto 描述的那样将密钥发送到元素。
回答by Vadim
.Net has a library to handle file upload dialog. It has a SendKeys class that has a method SendWait(string keys). It sends the given key on the active application and waits for the message to be processed. It does not return any value.
.Net 有一个库来处理文件上传对话框。它有一个 SendKeys 类,该类有一个方法 SendWait(string keys)。它在活动应用程序上发送给定的密钥并等待消息被处理。它不返回任何值。
回答by DalSoft
This can be done as follows, tested and working with Internet Explorer and Chrome driver
这可以按如下方式完成,测试并使用 Internet Explorer 和 Chrome 驱动程序
var allowsDetection = this.Driver as IAllowsFileDetection;
if (allowsDetection != null)
{
allowsDetection.FileDetector = new LocalFileDetector();
}
Driver.FindElement(By.Id("your-upload-input")).SendKeys(@"C:\PathToYourFile");
Reference https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ
参考https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ
回答by José Carlos
I used this to solve the problem... try it if all above does not works
我用它来解决问题...如果以上都不起作用,请尝试
Actions action = new Actions(driver);
action.SendKeys(pObjElement, Keys.Space).Build().Perform();
Thread.Sleep(TimeSpan.FromSeconds(2));
var dialogHWnd = FindWindow(null, "Elegir archivos para cargar"); // Here goes the title of the dialog window
var setFocus = SetForegroundWindow(dialogHWnd);
if (setFocus)
{
Thread.Sleep(TimeSpan.FromSeconds(2));
System.Windows.Forms.SendKeys.SendWait(pFile);
System.Windows.Forms.SendKeys.SendWait("{DOWN}");
System.Windows.Forms.SendKeys.SendWait("{TAB}");
System.Windows.Forms.SendKeys.SendWait("{TAB}");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
}
Thread.Sleep(TimeSpan.FromSeconds(2));
}

