0

I am using Selenium IDE. i try to search Samsung s3 images and right click on one image and click save image as then a window come and i don't know how to click save button their. my code is this

     WebDriver driver = new FirefoxDriver(); 
    driver.get("http://www.google.com");

    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys("samsung s3 picture");
    driver.findElement(By.name("btnG")).click();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.findElement(By.linkText("Images for samsung s3")).click();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    WebElement image=driver.findElement(By.name("JJJ3gKgkvtZE4M:"));


    Actions action= new Actions(driver);
    action.contextClick(image).build().perform();
    action.sendKeys(Keys.CONTROL, "v").build().perform();

      Robot robot = new Robot();

      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);
      robot.keyPress(KeyEvent.VK_DOWN);
      robot.keyRelease(KeyEvent.VK_DOWN);

      // To press Save button.
      robot.keyPress(KeyEvent.VK_ENTER);
      robot.keyRelease(KeyEvent.VK_ENTER);


      Alert alt=driver.switchTo().alert();
      Thread.sleep(3000);
      alt.accept();

i want to click that save button

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Waqar
  • 9
  • 5

3 Answers3

0

The popup displayed while saving image is put up by OS not by the browser. OS based popups can't be automated using selenium as it interacts only with the browser. You can use AutoIT tool for automating windows based popups. Hope it helps!

Harish
  • 306
  • 1
  • 4
  • 14
0

The best way to control such OS related events is to use Robot class.

       Robot robot = new Robot();  // Robot class throws AWT Exception  
       Thread.sleep(2000); // Thread.sleep throws InterruptedException  
       robot.keyPress(KeyEvent.VK_DOWN);  // press arrow down key of keyboard to navigate and select Save radio button  

       Thread.sleep(2000);  // sleep has only been used to showcase each event separately   
       robot.keyPress(KeyEvent.VK_TAB); 
       Thread.sleep(2000);  
       robot.keyPress(KeyEvent.VK_TAB); 
       Thread.sleep(2000);  
       robot.keyPress(KeyEvent.VK_TAB); 
       Thread.sleep(2000);  
       robot.keyPress(KeyEvent.VK_ENTER);   
   // press enter key of keyboard to perform above selected action  
BludShot
  • 301
  • 1
  • 3
  • 16
0

you don't need to use Robot to download a image! There are more elegant and faster ways of doing this. For example you could create a custom Firefox Profile and auto-download based on the mimetype..

there are already tons of answered questions about that, for example: access-to-file-download-dialog-in-firefox

you can also take a look on this link which is worth a read.

Community
  • 1
  • 1
metar
  • 434
  • 7
  • 17