具有隐藏输入的硒文件选择器[type = file] [英] Selenium File Chooser with a hidden input[type=file]

查看:144
本文介绍了具有隐藏输入的硒文件选择器[type = file]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片文件上传,想用 Selenium(java)来测试。用户有两个选项。他可以从本地机器上拖放图像,也可以点击浏览...按钮。然后出现浏览器指定的文件选择器。



Selenium 中,我尝试了不同的东西。据我所知,硒不能在浏览器之外运行。所以要测试拖放功能并不是一个可行的解决方案。所以我试图点击浏览按钮,并使用浏览器指定的文件选择器。我找到了一个 YouTube视频,某人在此对话框中成功选取了一个文件。他带着Robot类在键盘上做了一些事情。
$ b $ $ $ $ $ $ $ $ $ getDriver()。findElement(By.id(BROWSE_BUTTON))。点击();

Thread.sleep(5000);

StringSelection stringSelection = new StringSelection(C:\\A.JPG);
Toolkit.getDefaultToolkit()。getSystemClipboard()。setContents(stringSelection,null);

机器人机器人= new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);

当我调试这行时,第1行中的click b $ b我使用Firefox 31.1。



任何想法?也许有一些替代品?
输入中写入文件路径[type ='file'] 是不可替代的,因为这个输入是隐藏的。 Firefox Chrome 隐藏的文件输入不是一个问题 >。而且, v2.45.0.6 确保 IE 也可以使用隐藏文件输入。这么简单 sendkeys()使用 input [type ='file'] 应该可以。查看更新日志



编辑



对于FF,它也可能是一个兼容性问题。但是,最新版本的FF还不被 Selenium 支持。请参阅



2nd Edit

Selenium 2.46已经发布,IEDriver现在可以使用隐藏文件输入标签。


I have a image file upload and want to test it with Selenium (java). The user have two options. He can push the image with drag and drop from his local machine or he can click "Browse..." button. Then appears the browser specified file chooser.

In Selenium I tried different things. To my knowledge Selenium does not serve operations out of the browser. So to test the drag and drop functionality is not a possible solution.

So I tried to click the Browse button and work with the browser specified file chooser. I found a YouTube video where someone successfully pick a file on this dialog. He took the Robot class to do something on the keyboard.

getDriver().findElement(By.id(BROWSE_BUTTON)).click();

Thread.sleep(5000);

StringSelection stringSelection = new StringSelection("C:\\A.JPG");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);

When I debug this lines, the test is pending after the click() method in line 1. I use Firefox 31.1.

Any ideas? Maybe there are some alternatives? To write the file path in the input[type='file'] is no alternative because this input is hidden.

解决方案

Hidden file input has not been an issue with Firefox or Chrome. And, v2.45.0.6 makes sure the IE works with hidden file input as well. So simple sendkeys() using input[type='file'] should be fine for you. See changelog

EDIT

For FF it could be a compatibility issue as well. But, latest version of FF is not yet supported by Selenium. See this

2nd Edit

Selenium 2.46 has been released and IEDriver should work with hidden file input tag now.

这篇关于具有隐藏输入的硒文件选择器[type = file]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆