【发布时间】:2012-02-09 16:26:08
【问题描述】:
我正在使用 selenium webdriver,C#。
是否可以使用 Firefox 选择文件对话框制作工作 webdriver? 还是我必须使用 AutoIt 之类的东西?
【问题讨论】:
我正在使用 selenium webdriver,C#。
是否可以使用 Firefox 选择文件对话框制作工作 webdriver? 还是我必须使用 AutoIt 之类的东西?
【问题讨论】:
如果您尝试选择要上传的文件,Selenium 2 支持 HTML 文件输入。例如:
HTML
<input type="file" id="uploadhere" />
硒代码
IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");
基本上你“输入”(使用SendKeys)文件输入元素的完整文件路径。 Selenium 为您处理文件选择对话框。
但是,如果您想操作任意文件选择对话框,那么就像 Anders 所说,您必须在 Selenium 之外进行操作。
【讨论】:
不,WebDriver 不能与对话框交互 - 这是因为对话框是操作系统的域,而不是网页。
我知道有人对 autoit 以及 .Net 提供的自动化 API 很满意。
另一种选择是完全跳过文件对话框并发出 POST 或 GET,但这需要更高级的网站知识以及如何构建 POST/GET。
你可以试试Webinator,它类似于 Selenium,因为它是由 WebDriver 驱动的。它提供了文件对话功能,我已经取得了巨大的成功。
【讨论】:
这是另一个使用 remotewebdriver 的解决方案,它就像魔术一样工作,我喜欢它。
这是我的课程:
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();
【讨论】:
您要求在文件对话框中使用 AutoIt。这很简单,您可以使用 C# 来完成。
安装nuget包AutoItX.Net
使用下面的演示代码
根据需要更改对话框标题字符串
public static void InsertIntoFileDialog(string file, int timeout = 10)
{
int aiDialogHandle = AutoItX.WinWaitActive("Save As", "", timeout); // adjust string as you need
if (aiDialogHandle <= 0)
{
Assert.Fail("Can't find file dialog.");
}
AutoItX.Send(file);
Thread.Sleep(500);
AutoItX.Send("{ENTER}");
Thread.Sleep(500);
}
在我遇到与文件对话框相关的 Appium/Selenium 问题后,这对我有帮助。
【讨论】:
.Net 有一个库来处理文件上传对话框。它有一个 SendKeys 类,该类有一个方法 SendWait(string keys)。它在活动应用程序上发送给定的密钥并等待消息被处理。它不返回任何值。
【讨论】:
这可以通过以下方式完成,测试并使用 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");
参考https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ
【讨论】:
如果您想上传文件而不使用 WebDriver,我遇到的唯一解决方案是 AutoIt。它允许您编写脚本并将其转换为可执行文件,然后您可以从代码中调用它。我在使用 ActiveX 控件时成功使用了它。
【讨论】:
另一种方法是使用System.Windows.Forms.SendKeys.SendWait("pathToFile")。
我在无法像@prestomanifesto 描述的那样向元素发送密钥的任何地方都成功使用它。
【讨论】:
我用这个来解决问题...如果以上都不起作用,请尝试
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));
}
【讨论】: