1

I'm using Selenium WebDriver to automate a web browser, but I have a test case in which I need to save a pdf. Inspired by the methods employed in these two questions question 1 question 2 I have got the "Save As" to pop up with a java Robot object and pressed control + s by:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);

This works great; however, when I try to type a location in the save to box and press enter with this code:

robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

it doesn't do anything. How come I can't type POC in the save to box and press enter using the same technique when it clearly has the focus?

Community
  • 1
  • 1
Nick L
  • 281
  • 1
  • 6
  • 18

1 Answers1

1

What was happening was that the robot was moving too fast for the browser. I added some robot.delay(x)s and I changed the keyPress and keyRelease calls around a little bit. Here is the working code:

Robot robot = new Robot();
robot.delay(3000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_C);
robot.delay(50);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Nick L
  • 281
  • 1
  • 6
  • 18