Download And Save Image Using Selenium WebDriver + Actions + Robot

Downloading Image from web page Is easy but It Is tricky task  In selenium WebDriver. Previously I described usages of Actions class of selenium WebDriver In test cases to perform tricky actions. You can find all Actions class examples link on THIS PAGE. under advanced user
Interaction API tutorial section. For downloading Image, We will use same WebDriver Actions class with java robot class.
For saving Image from web page In selenium webdriver, We have to perform bellow given actions.
  • Right click on Image
  • Select "Save Image As" option from mouse right click context menu.
  • Enter file name In save Image dialog
  • Press Save button.
Right click on Image using contextClick() method of Actions class
As you know, "Save Image As" option will display when you right click on any Image. We will use contextClick() method of WebDriver Actions class to right click on Image as shown In bellow Image.

Code to right click on Image Is as bellow.
//Locate Image
WebElement Image =driver.findElement(By.xpath("//img[@border='0']"));
//Rihgt click on Image using contextClick() method.
Actions action= new Actions(driver);
action.contextClick(Image).build().perform();

Select "Save Image As" option
If you see In Image, We can select "Save Image As" option using CONTROL + V from keyboard. To do It In selenium webdriver, We will use bellow given code.
//To perform press Ctrl + v keyboard button action.
action.sendKeys(Keys.CONTROL, "v").build().perform();

It will open Save Image dialog.


Why need robot class?

As you know, Actions class can help you to perform any action on web page only. It can not handle any windows dialog like Save Image. We can use Java Robot class to handle Save Image dialog. Robot class Is very good feature of Java using which we can send keyboard key press and key release events very easily to enter file name and press Save button on Save Image dialog. Code to enter file name "D:\test" and press Save button on Save Image dialog Is as bellow.
Robot robot = new Robot();
// To press D key.
robot.keyPress(KeyEvent.VK_D);
// To press : key.
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
// To press \ key.
robot.keyPress(KeyEvent.VK_BACK_SLASH);
// To press "test" key one by one.
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_T);
// To press Save button.
robot.keyPress(KeyEvent.VK_ENTER);

This way, We can save Image using Actions class of WebDriver and Robot class of java. Full practical example Is as bellow.
package Testing_Pack;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Actions_Test {
 
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/09/selectable.html");
 }
 
 @Test
 public void Save_Image() throws IOException, InterruptedException, AWTException {
  //Locate Image
  WebElement Image =driver.findElement(By.xpath("//img[@border='0']"));
  //Rihgt click on Image using contextClick() method.
  Actions action= new Actions(driver);
  action.contextClick(Image).build().perform();
  
  //To perform press Ctrl + v keyboard button action.
  action.sendKeys(Keys.CONTROL, "v").build().perform();

  Thread.sleep(3000);
  Robot robot = new Robot();
  // To press D key.
  robot.keyPress(KeyEvent.VK_D);
  // To press : key.
  robot.keyPress(KeyEvent.VK_SHIFT);
  robot.keyPress(KeyEvent.VK_SEMICOLON);
  robot.keyRelease(KeyEvent.VK_SHIFT);
  // To press \ key.
  robot.keyPress(KeyEvent.VK_BACK_SLASH);
  // To press "test" key one by one.
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyPress(KeyEvent.VK_S);
  robot.keyPress(KeyEvent.VK_T);
  // To press Save button.
  robot.keyPress(KeyEvent.VK_ENTER);  
 }
}

VIEW EXAMPLE of robot class to close browser tabs.

10 comments:

  1. Hi Arvind first of all iam very thank full for the above scenario

    but i want to just add is that it is not CONTROL + V but it is SHIFT +V
    thanks
    pavan

    ReplyDelete
  2. Hi Arvind first of all iam very thank full for the above scenario

    but i want to just add is that it is not CONTROL + V but it is SHIFT +V
    thanks
    pavan

    ReplyDelete
  3. Where this Image is going to be store on local disk?

    ReplyDelete
  4. Can we Right click using Robot Class and if yes then how to do it?

    ReplyDelete
  5. Is it possible to Right Click using Robot Class and if yes then how to do it

    ReplyDelete
  6. Hi Aravind,
    Thanks a lot !! solves my inital problem.
    But now problem is i want change file name every time..i.e should be unique each time..plz suggest solution.

    ReplyDelete
  7. Hi, After doing right click how can we access buttons on the bar which appears. For example: clicking refresh or save as

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete