简体   繁体   English

如何从Selenium Webdriver Java中的文件夹上传图像

[英]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以及其他必要的进口:

Edited the Code for multiple file upload (Works in FF, IE and Chrome): 编辑了用于上传多个文件的代码(适用于FF,IE和Chrome):

//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.

相关问题 使用Selenium WebDriver(Java)上传本地图像 - Upload Local Image With Selenium WebDriver (Java) 如何在Java中使用Selenium WebDriver上传文件 - How to upload file using Selenium WebDriver in Java 如何使用Selenium WebDriver和Java从图像(验证码)中读取文本 - How to read the text from image (captcha) by using Selenium WebDriver with Java 在 Java Maven 项目中,如何使用 selenium 从资源文件夹上传文件(照片) - In Java Maven project how do I upload file (photo) from resource folder using selenium 如何使用Selenium Webdriver中的“上传图像按钮”上传文件/照片 - How to Upload File/Photo using “Upload Image Button” in Selenium Webdriver 如何在Selenium Java Webdriver中验证图像的尺寸? - How to verify dimensions of image in Selenium Java Webdriver? 如何使用 Java 设置 Selenium WebDriver? - How do I setup Selenium WebDriver with Java? 如何使用Selenium WebDriver处理Firefox上载文件窗口-Java - How to handle Firefox Upload File window with Selenium WebDriver - Java 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 在Mac OS上使用Selenium Webdriver上传图像 - Image Upload using selenium webdriver on mac OS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM