Shantanu Nandan Shantanu Nandan - 1 month ago 12
Java Question

Not able to save image at desired location/folder using AutoIT with Selenium Webdriver

I am trying to download an image from a website using AutoIT(to control OS Pop up Window) and Selenium Webdriver(to open the website from where i am trying to download the pic).

I am getting the OS Pop Up window and by using AutoIT i am able to send the new location for saving the file i.e ,

C:\Users\Casper\Desktop\Resume\Pic.jpg


But once the script clicks on the save button the pic get downloaded but with a different name and at a different/default location.

AutoIT Script which i am using is written below-

WinWait("Save As");
WinActive("Save As");
Sleep(1000);
ControlSetText("Save As","","[CLASS:Edit; INSTANCE:1]","C:\Users\Casper\Desktop\Resume\Pic.jpg");
Sleep(1000);
ControlClick("Save As","","[CLASS:Button; INSTANCE:1]");
Sleep(1000);


Java code for Webdriver-

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Practice {

public void pic() throws AWTException, IOException, InterruptedException{

WebDriver driver;
System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
driver = new ChromeDriver();
try{
driver.navigate().to("http://i.stack.imgur.com/rKZOx.jpg?s=128&g=1");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("/html/body/img"))).perform();
action.contextClick().perform();
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
// Here i am getting the os window but don't know how to send the desired location
String command ="C:\\Users\\Casper\\Desktop\\Resume\\Pic.exe";
Runtime.getRuntime().exec(command);

}catch(Exception e){
e.printStackTrace();
driver.close();
}//catch
finally{
Thread.sleep(6000);
System.out.println("command");
driver.quit();
System.exit(0);
}
}//method


ScreenShot during exxecution of Program

As you can see it is succsesfully sending the new address to the OS Window Pop (inside red circle) but after clicking on Save button the image is getting downloaded at different location
C:\Users\Casper\Downloads (my default download folder)
with a different name -rKZOx

Answer

Now i got the answer. Just because i was not waiting for the window to open properly i was not able to download the file at my desired location. I just put a Thread wait for 2 seconds and now its working fine and saving the image at the desired location. Changed code is-

Rest of the code remain same the below code is changed -

  Thread.wait(2000);
   String command ="C:\\Users\\Casper\\Desktop\\Resume\\Pic.exe";
    Runtime.getRuntime().exec(command);

And now i am able to save image at e drive with the name of the file as pic