首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Selenium WebDriver和浏览器选择文件对话框

Selenium WebDriver和浏览器选择文件对话框
EN

Stack Overflow用户
提问于 2012-01-13 21:06:19
回答 6查看 42.1K关注 0票数 25

我正在使用selenium webdriver,C#。

有没有可能使工作webdriver与Firefox选择文件对话框?或者我必须使用像AutoIt这样的东西?

EN

回答 6

Stack Overflow用户

发布于 2012-01-13 23:12:57

不,WebDriver不能与对话框交互-这是因为对话框是操作系统的域,而不是网页。

我认识一些使用autoit以及.Net提供的Automation API的人。

另一种选择是完全跳过文件对话框,发出POST或GET,但这需要更高级的网站知识以及理解如何构造POST/GET。

你可以试试Webinator,因为它是由WebDriver驱动的,所以它类似于Selenium。它提供了文件对话功能,我用它取得了很大的成功。

票数 10
EN

Stack Overflow用户

发布于 2012-07-13 17:32:30

这是另一个使用remotewebdriver的解决方案,它像魔术一样工作,我喜欢它。

下面是我的类:

代码语言:javascript
复制
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(); 
票数 4
EN

Stack Overflow用户

发布于 2016-07-20 00:26:00

可以按如下方式完成此操作,并使用Internet Explorer和Chrome驱动程序进行测试和工作

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8851051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档