Friday, 4 April 2014

Java AWT Robot | Selenium Uses

Note|  Text in Blue [#PYTHON] and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.

Java Robot

This is the continuation of one my previous topics, link here.  Selenium doesn't give support on automating or handling Browser pop-ups and Native OS pop-ups.

There are 3 ways to make it work.
     1| Java.awt.Robot Toolkit
     2| 3rd party tools like AutoIT [check link here]
     3| Browser Profile [check link here]

Fig. Browser PopUp

Java Robot handles Mouse Actions and Keyboard Actions. For example, some of the most important functions are as follows:

      1| keyPress()
      2| keyRelease()
      3| mousePress()
      4| mouseRelease()
      5| Mousemove()

Handle Browser Pop-up

1| The below snippet is used to save a file from Download Browser Pop-up shown above in Fig.
2| The below co-ordinates are set for Screen resolution | 1920 x 1080
3| Window status | Maximized
4| Customize the xy co-ordinates in-case if you are using different screen resolution

Robot r = new Robot();
// click Save File 
r.mouseMove(787, 544); // move to co-ordinate Location
r.mousePress(InputEvent.BUTTON1_MASK); // Left Mouse click - Press
r.mouseRelease(InputEvent.BUTTON1_MASK); // Left Mouse click - Release 
r.delay(5); // wait for 5 millisecs
// click ok
r.mouseMove(1032, 641); // move to co-ordinate Location
r.mousePress(InputEvent.BUTTON1_MASK); // Left Mouse click - Press
r.mouseRelease(InputEvent.BUTTON1_MASK); // Left Mouse click - Release

Robot Functions

InputEvent.BUTTON1_MASK // Left button
InputEvent.BUTTON2_MASK // Middle button
InputEvent.BUTTON3_MASK // Right button

Middle click | Press & Release
Robot r = new Robot();

Right click | Press & Release

Scroll Mouse

Pixel Color | RBG
System.out.println(r.getPixelColor(923, 87));

Get Current Mouse position [location]

Get Screen Resolution | Dimension

Screen Capture
java.awt.Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(new Rectangle(size));
File path = new File("C://screen.jpg");
ImageIO.write(img, "JPG", path);
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(new Rectangle(0, 0, 100, 100));
File path = new File("C://screen.jpg");
ImageIO.write(img, "JPG", path);

Enter/Type Text
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;

Robot r = new Robot();     
typeKeys("Prashanth Sams", r);

public static void typeKeys(String str,Robot r)
 for(int i=0;i<str.length();i++)
  typeCharacter(r, ""+str.charAt(i));
public static void typeCharacter(Robot robot, String letter)
  boolean upperCase = Character.isUpperCase( letter.charAt(0) );
  String variableName = "VK_" + letter.toUpperCase();
  Class clazz = KeyEvent.class;
  Field field = clazz.getField( variableName );
  int keyCode = field.getInt(null);


  if (upperCase) robot.keyPress( KeyEvent.VK_SHIFT );

  robot.keyPress( keyCode );
  robot.keyRelease( keyCode );

  if (upperCase) robot.keyRelease( KeyEvent.VK_SHIFT );
 catch(Exception e)

Keyboard Actions
r.keyPress(KeyEvent.VK_ENTER); // Press Enter Key

Find all/rest of the Java Robot keyboard actions here
Refer| Robot

Mofiki's Coordinate Finder will help you to get the screen coordinates.


  1. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.


  2. There is a great blog about selenium and thanks for sharing about selenium this information is used for to now the selenium commands and sevices seleniumonlinetraining

  3. Thanks for nice topic. Very useful information.
    IthubOnlineTraining are good in giving the selenium onlineTraining

  4. Thanks for the great information in your blog Selenium Training in Chennai


  5. Thank u for your information. I read your shared information on selnium Topic.
    Selenium Online Training

  6. Robot doesn't work while handling download popup in IE browser .Any idea ?

  7. Well explained. Got to learn new things from your Blog on Appium.
    Appium training in chennai

  8. I cant wait to check out some of these blogs! I’ve really wanted to start learning more about cars and auto repairs lately and I think this will help a lot. I think it can save my family some money if we knew how to do some repairs at home.! Thanks again for all the options.

    bike spa services in mumbai
    house cleaning services in mumbai
    car wash services in mumbai

  9. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.selenium training in chennai


  10. Thanks for posting this useful content, Good to know about new things here, Let me share this, . VMWare training in pune

  11. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

  12. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

  13. I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.

    Digital Marketing Company in Chennai

  14. Very amazing blog on a fresh new topic. I am definitely going to use the information provided in this blog because of its novel and fresh. Thanks for sharing such valuable information with everyone.
    Website development company in bangalore
    Web designers in Bangalore
    ECommerce Website developers in bangalore
    Outsource magento ecommerce services india