无法使用机械手类和Sendkeys上传文件 [英] Not able to upload file using robot class and Sendkeys

查看:88
本文介绍了无法使用机械手类和Sendkeys上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用机械手类和sendkey上传文件.

I am not able to upload file using robot class and sendkeys.

下面是我尝试过的代码

package garbage;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class imageupload {

    static WebDriver driver;
    static String baseURL="http://elance.wetwaresoft.com/account/register";


    public static void main(String[] args) throws AWTException, InterruptedException, IOException 
    {
        driver=new FirefoxDriver();
        driver.get(baseURL);
        WebElement btn=driver.findElement(By.xpath("//*[@class='fileupload-new']"));
        btn.click();
        System.out.println("Going in Robot class");
        Runtime.getRuntime().exec("notepad");
        Robot r=new Robot();
        r.delay(1000);
        r.keyPress(KeyEvent.VK_ENTER); 
        r.keyPress(KeyEvent.VK_D);
        r.keyPress(KeyEvent.VK_SHIFT);
        r.keyPress(KeyEvent.VK_SEMICOLON);
        r.keyRelease(KeyEvent.VK_SHIFT);
        r.keyPress(KeyEvent.VK_BACK_SLASH);
        r.keyPress(KeyEvent.VK_A);
        r.keyPress(KeyEvent.VK_DOWN);
        r.keyPress(KeyEvent.VK_ENTER);
        System.out.println("File uploaded");
    }
}

当我在记事本中执行我的机器人类代码时,它会键入路径,但是当我在窗口弹出窗口中执行时,它没有输入任何内容.

When I am executing my robot class code in notepad then it types the path but when I execute this in a window popup then it is not typing anything.

如何使用机械手和sendkeys上传文件?

How can I upload file using robot and sendkeys?

推荐答案

我已经使用 Robot 将文件upalod逻辑分离到单独的类中,并使方法静态化.在方法fileAttachmentUsingRobot中,将路径传递到要上传的图像,即图像的完整系统路径.

I've separated file upalod logic using Robot into separate class, and made methods static. In method fileAttachmentUsingRobot to pass path to image you want to be uploaded, i.e full system path to image.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

/**
 * Created by ypolshchykau on 30.01.2015.
 */
public class FileAttacherRobotImplementation {
    public final static Logger log = LoggerFactory.getLogger(FileAttacherRobotImplementation.class);

    /**
     * this method implements file attachment using Robot mechanism
     *
     * @param filePathToImage
     */

    public static void fileAttachmentUsingRobot(String filePathToImage) {
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        setClipboardData(filePathToImage);
        robot.delay(1000);

        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);
    }

    /**
     * method for copying file in order to attach file in File open window
     *
     * @param str
     */
    public static void setClipboardData(String str) {
        StringSelection stringSelection = new StringSelection(str);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

}

希望这对您有所帮助.

这篇关于无法使用机械手类和Sendkeys上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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