[英]how to do image upload from a folder in selenium webdriver java
I am automating mobile website using selenium. 我正在使用硒自动化移动网站。 In my test case I need to upload image.
在我的测试案例中,我需要上传图片。 There is one camera image with id addimage on click of which file upload popup is shown.
单击时,将显示一个ID为addimage的摄像机图像,其中显示了文件上传弹出窗口。 Check images of this flow
查看此流程的图像
HTML code for this : 为此的HTML代码:
<div class="clearfix">
<ul id="imageList"> </ul>
<div id="uploadimginput">
<a id="addimage" class="sprite camera"> </a>
</div>
<input id="image" class="w0" type="file" name="image">
</div>
Multiple image upload : 多张图片上传:
From file upload popup i want to open a folder "testimages" and then select an image. 从文件上传弹出窗口中,我想打开一个文件夹“ testimages”,然后选择一个图像。
How can I do this in selenium java. 我如何在Selenium Java中做到这一点。
Don't click on the image itself so that the popup will not appear. 不要单击图像本身,以免出现弹出窗口。
In your html code there should be an input element with type file. 在您的html代码中,应该有一个类型为file的输入元素。 In your Selenium test you can find the input element and fill it with the path of the image you want to add.
在Selenium测试中,您可以找到输入元素,并在其中添加要添加的图像的路径。 Then submit the form around the input element.
然后围绕输入元素提交表单。
The Selenium framework will handle the rest for you. Selenium框架将为您处理其余的工作。 For me it works fine with all browsers.
对我来说,它适用于所有浏览器。
I think its a cleaner solution than simulation a keyboard. 我认为这是比模拟键盘更干净的解决方案。
From what I can see, this is a Window dialog box and it is out of the context of browser, and hence can't be automated directly by Selenium. 据我所知,这是一个“ 窗口”对话框 ,它不在浏览器的上下文中,因此Selenium无法直接将其自动化。 Hence, you will have to use Robot/Sikuli/Autoit for that.
因此,您将必须使用Robot / Sikuli / Autoit 。
Below code is the way by using "Robot class" . 下面的代码是使用“机器人类”的方式 。 For using this, do import all the classes from "java.awt package" namely java.awt.Robot, java.awt.event.KeyEvent, java.awt.Toolkit, java.awt.datatransfer.StringSelection, java.awt.AWTException alongwith the rest necessary imports:
为此,请从“ java.awt程序包”中导入所有类,即java.awt.Robot,java.awt.event.KeyEvent,java.awt.Toolkit,java.awt.datatransfer.StringSelection,java.awt.AWTException以及其他必要的进口:
//Code for clicking on the image button that brings up the window dialog box
...
//Putting all the absolute paths of the pics to upload(here, 3 files)
String arr[] = {"\"D:\\Pic1.jpg\"", "\"D:\\Pic2.jpg\"", "\"D:\\Pic3.jpg\""};
//Copying the path of the file to the clipboard
StringSelection photo = new StringSelection(arr[0]+arr[1]+arr[2]); //Putting the path of the image to upload
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(photo, null);
//Pasting the contents of clipboard in the field "File name" of the Window Pop-up
Thread.sleep(5000); //Some sleep time to detect the window popup
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//To Click on the "Open" button to upload files
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.