Java的selenium代码随笔(2)

时间:2021-09-11 17:29:27

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

public class RobotOperation {

//初始化
public RobotOperation() {

}

/*
* 此方法主要用户粘贴数据
*/
public void SetPasteData(String PasteData) {
//声明stringselection对象,并是用函数string参数完成实例化
StringSelection stringSelection = new StringSelection(PasteData);
//使用ToolKit对象的setContents方法将字符串放到剪贴板中
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
//声明Robot对象
Robot robot = null;
try {
//生产Robot的对象实例
robot = new Robot();
} catch (AWTException e) {
// TODO: handle exception
e.printStackTrace();
}
//调用keypress方法来实现按下Ctrl键
robot.keyPress(KeyEvent.VK_CONTROL);
//调用keypress方法来实现按下Ctrl键
robot.keyPress(KeyEvent.VK_V);
//调用keyrelease方法来实现释放Ctrl键
robot.keyRelease(KeyEvent.VK_V);
//调用keyrelease方法来实现按下V键
robot.keyRelease(KeyEvent.VK_CONTROL);
//调用keypress方法来实现按下Enter键
robot.keyPress(KeyEvent.VK_ENTER);
//调用keyrelease方法来实现释放Enter键
robot.keyRelease(KeyEvent.VK_ENTER);
}
}