简体   繁体   English

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

[英]using a robot to save a web page with different names

I am using selenium to create a robot that opens a page and save it automatically, as follow: 我正在使用selenium创建一个打开页面并自动保存的机器人,如下所示:

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

There are two problems, first of all, it actually does not press enter and only opens the save as window and second, how can I make it to pass a different name or at least, do not override pages when the file name are same? 有两个问题,首先,它实际上不按Enter键而是仅打开save as窗口,其次,如何使它传递不同的名称,或者至少在文件名相同时不覆盖页面?

You are right. 你是对的。 When we use driver.getPageSource() the css, scripts and related resources do not get saved and cannot be viewed offline properly. 当我们使用driver.getPageSource()时,css,脚本和相关资源无法保存,因此无法正确地离线查看。

I was able to save the file using the same code. 我能够使用相同的代码保存文件。 It was only a matter of adding a Thread.sleep() after each operation. 只需在每个操作之后添加Thread.sleep()

Notice that when the Save As window opens up the, focus is on the file name. 请注意,当“另存为”窗口打开时,焦点将放在文件名上。 So you can use the Robot class to input a file name. 因此,您可以使用Robot类输入文件名。 Regarding your problem with the file name should not get overwritten you can use a random number generator. 关于您的文件名问题不应该被覆盖,您可以使用随机数生成器。 You might have to create a function for making this task easy. 您可能需要创建一个函数来简化此任务。

Please check the code below. 请检查下面的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM