使用机器人来保存具有不同名称的网页

时间:2016-01-24 04:00:12

标签: java url selenium autosave

我正在使用public static void main(String[] args) { Date nextSetDate = getTodayAtMidnight(); System.out.println(nextSetDate); Date now = new Date(); System.out.println(now.after(nextSetDate)); } public static Date getTodayAtMidnight() { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); c.add(Calendar.DAY_OF_YEAR, 1); return c.getTime(); } 创建一个打开页面并自动保存的机器人,如下所示:

selenium

有两个问题,首先,它实际上没有按Enter键只打开 WebDriver driver = new FirefoxDriver(); driver.get("http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7043856"); Robot robot = new Robot(); robot.delay(20000); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_S); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); 窗口,其次,如何让它传递一个不同的名称,或者至少,不要覆盖页面时文件名是一样的吗?

1 个答案:

答案 0 :(得分:1)

你是对的。当我们使用driver.getPageSource()时,css,脚本和相关资源不会被保存,也无法正常脱机查看。

我能够使用相同的代码保存文件。只需在每次操作后添加Thread.sleep()即可。

请注意,当“另存为”窗口打开时,焦点位于文件名上。因此,您可以使用Robot类输入文件名。关于您的文件名问题不应该被覆盖,您可以使用随机数生成器。您可能需要创建一个功能来简化此任务。

请检查以下代码。

public static void main(String[] args) throws AWTException, InterruptedException {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7043856");

    // Added this line to let the page load completely
    String pageSource = driver.getPageSource();

    Robot robot = new Robot();

    // Press Ctrl+S
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_S);

    Thread.sleep(5000);

    // Generate a 2 digit random number and split it into two separate chars
    String random = RandomStringUtils.randomNumeric(2);
    System.out.println(random);
    char charOne = random.charAt(0);
    char charTwo = random.charAt(1);

    // Save As window has opened and the focus is on the file name field.
    // Click right arrow key to go to the last of the already present name
    robot.keyPress(KeyEvent.VK_RIGHT);
    robot.keyRelease(KeyEvent.VK_RIGHT);

    // Append the generated random number to the name
    robot.keyPress(getKeyEvent(charOne));
    robot.keyRelease(getKeyEvent(charOne));
    robot.keyPress(getKeyEvent(charTwo));
    robot.keyRelease(getKeyEvent(charTwo));

    Thread.sleep(5000);

    // Press enter
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

}

public static int getKeyEvent(char key) {

    switch (key) {
    case '1':
        return KeyEvent.VK_1;
    case '2':
        return KeyEvent.VK_2;
    case '3':
        return KeyEvent.VK_3;
    case '4':
        return KeyEvent.VK_4;
    case '5':
        return KeyEvent.VK_5;
    case '6':
        return KeyEvent.VK_6;
    case '7':
        return KeyEvent.VK_7;
    case '8':
        return KeyEvent.VK_8;
    case '9':
        return KeyEvent.VK_9;
    default:
        return KeyEvent.VK_0;
    }
}