Java Robot 自动打开chrome并访问CSDN官网

一、主要操作流程

win + r                 打开运行
cmd + enter             输入命令回车进入dos
start chrome + enter    输入命令回车启动chrome
alt + space + x         窗口最大化
ctrl + l                切换到浏览器搜索栏
www.csdn.net + enter    输入网址
                        简单案例完成

二、代码如下

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 *
 * @author NingZe
 * description: nz robot
 * date: 2019/8/15 0015 12:13
 * version: 02.06
 * To change this template use File | Settings | File Templates.
 */
public class NzRobot {

    public static void main(String[] args) {

        NzRobot nzRobot = new NzRobot();

        // 启动google
        nzRobot.startChrome();

        // 延时1秒执行
        robot.delay(1000);

        // 控制窗口
        nzRobot.nzSimulationRobot();

    }

    /**
     * 机器人 - 操作用户界面
     */
    public static Robot robot;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 内存管理
     */
    public static Runtime runtime = Runtime.getRuntime();

    /**
     * 随机数
     */
    public static Random random = new Random();

    public String nzSimulationRobot() {

        login();

        return "nzSimulationRobot";
    }

    private static void login() {

        System.err.println("自动登录机器人已启动");

        // 窗口最大化
        automaticFullScreen();

        // 切换到浏览器搜索栏
        pressCtrlAndKey(KeyEvent.VK_L);

        // 输入 www.csdn.net
        pressKey(KeyEvent.VK_W);
        pressKey(KeyEvent.VK_W);
        pressKey(KeyEvent.VK_W);
        pressKey(KeyEvent.VK_DECIMAL);
        pressKey(KeyEvent.VK_C);
        pressKey(KeyEvent.VK_S);
        pressKey(KeyEvent.VK_D);
        pressKey(KeyEvent.VK_N);
        pressKey(KeyEvent.VK_DECIMAL);
        pressKey(KeyEvent.VK_N);
        pressKey(KeyEvent.VK_E);
        pressKey(KeyEvent.VK_T);

        // 回车
        pressKey(KeyEvent.VK_ENTER);

        // 延时1.5-2.5秒执行
        // robot.delay(produceWhilesRandomNum(1.5, 2.5, 1000));

        // 1860 * 120 去登录按钮
        // robot.mouseMove(1860, 120);

        // 单击
        // oneClick();

    }

    private static void logins() {

        System.err.println("全速自动登录机器人已启动");

        // 窗口最大化
        automaticFullScreen();

        // 切换到浏览器搜索栏
        pressCtrlAndKey(KeyEvent.VK_L);

        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));

        // 输入 www.csdn.net
        pressKeys(KeyEvent.VK_W);
        pressKeys(KeyEvent.VK_W);
        pressKeys(KeyEvent.VK_W);
        pressKeys(KeyEvent.VK_DECIMAL);
        pressKeys(KeyEvent.VK_C);
        pressKeys(KeyEvent.VK_S);
        pressKeys(KeyEvent.VK_D);
        pressKeys(KeyEvent.VK_N);
        pressKeys(KeyEvent.VK_DECIMAL);
        pressKeys(KeyEvent.VK_N);
        pressKeys(KeyEvent.VK_E);
        pressKeys(KeyEvent.VK_T);

        // 回车
        pressKeys(KeyEvent.VK_ENTER);

        // 延时1.5-2.5秒执行
        // robot.delay(produceWhilesRandomNum(1.5, 2.5, 1000));

        // 1860 * 120 去登录按钮
        // robot.mouseMove(1860, 120);

        // 单击
        // oneClick();

    }

    /**
     * 启动chrome浏览器
     */
    private void startChrome() {

        // 启动chrome浏览器
        // runtime.exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

        robot.keyPress(KeyEvent.VK_WINDOWS);
        robot.keyPress(KeyEvent.VK_R);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        robot.keyRelease(KeyEvent.VK_R);
        robot.keyRelease(KeyEvent.VK_WINDOWS);

        robot.delay(produceWhilesRandomNum(0.5, 1.0, 1000));

        // 输入cmd
        pressKeys(KeyEvent.VK_C);
        pressKeys(KeyEvent.VK_M);
        pressKeys(KeyEvent.VK_D);

        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        // 回车
        pressKeys(KeyEvent.VK_ENTER);

        // 延时0.5-1.5秒执行
        robot.delay(produceWhilesRandomNum(0.5, 1.5, 1000));

        // 输入start
        pressKeys(KeyEvent.VK_S);
        pressKeys(KeyEvent.VK_T);
        pressKeys(KeyEvent.VK_A);
        pressKeys(KeyEvent.VK_R);
        pressKeys(KeyEvent.VK_T);

        // 空格
        pressKeys(KeyEvent.VK_SPACE);

        // 输入chrome
        pressKeys(KeyEvent.VK_C);
        pressKeys(KeyEvent.VK_H);
        pressKeys(KeyEvent.VK_R);
        pressKeys(KeyEvent.VK_O);
        pressKeys(KeyEvent.VK_M);
        pressKeys(KeyEvent.VK_E);

        // 回车
        pressKeys(KeyEvent.VK_ENTER);

    }

    /**
     * 按Key - 单键
     *
     * @param Key
     */
    private static void pressKey(int Key) {
        // 点击键
        robot.keyPress(Key);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        // 弹起键
        robot.keyRelease(Key);
    }

    /**
     * 按Key - 无间隔单键
     *
     * @param Key
     */
    private static void pressKeys(int Key) {
        // 点击键
        robot.keyPress(Key);
        // 弹起键
        robot.keyRelease(Key);
    }

    /**
     * 按Ctrl+Key - 组合键
     *
     * @param Key
     */
    private static void pressCtrlAndKey(int Key) {
        // 点击键Ctrl+Key
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(Key);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        // 弹起键Ctrl+Key
        robot.keyRelease(Key);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }

    /**
     * 单击
     */
    private static void oneClick() {
        robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
    }

    /**
     * 双击
     */
    private static void twoClick() {
        robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
        robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
        // 延时0.2-0.4秒执行
        robot.delay(produceWhilesRandomNum(0.2, 0.4, 1000));
        robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
    }

    /**
     * 自动化-最大化窗口,模拟按 Alt+ 空格 + X
     * 切记当同时按有顺序的组合键时,应该在间隔处添加细微的延时,否则容易引起失败(因为按键速度太快,导致混乱)
     */
    public static void automaticFullScreen() {
        try {
            robot.keyPress(KeyEvent.VK_ALT);
            robot.delay(200);
            robot.keyPress(KeyEvent.VK_SPACE);
            robot.delay(200);
            robot.keyPress(KeyEvent.VK_X);
            robot.delay(200);
            robot.keyRelease(KeyEvent.VK_X);
            robot.delay(200);
            robot.keyRelease(KeyEvent.VK_SPACE);
            robot.delay(200);
            robot.keyRelease(KeyEvent.VK_ALT);
            robot.delay(200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成指定区间的随机数
     *
     * @param r1
     * @param r2
     * @param num
     * @return
     */
    private static int produceWhilesRandomNum(double r1, double r2, int num) {
        return (int) ((r1 + random.nextDouble() * (r2 - r1)) * num);
    }

}

三、复制粘贴

    /**
     * 复制并粘贴
     *
     * @param msg
     */
    private static void copyAndPaste(String msg) {
        // 复制(内容为 msg)
        StringSelection stringSelection = new StringSelection(msg);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, stringSelection);
        // 粘贴
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值