Re: trouble in upload images(photos) at Selenium Web Driver using Java

2,259 views
Skip to first unread message

Umamaheshwar Thota

unread,
Apr 15, 2013, 10:20:37 AM4/15/13
to webd...@googlegroups.com
Hi Vara,

Actually Selenium Webdriver will not support window based actions, like selecting Photos, files etc. For this you need to use the 3rd party tools like AutoIT. Try using AutoIT for uploading your image.

Cheers,
Mahesh


On Sunday, April 14, 2013 11:22:00 AM UTC+5:30, Dora vara wrote:
Hi,

I am new to Selenium webdriver. I tried to upload some pictures at Gmail. I signed in to Gmail. Clicked on Google+. Clicked AddPhoto. Windows Explorer dialog box pops up. Everything works fine till here. Now I cannot select the required *.jpg file from the windows explorer.  Any help to solve it is appreciated.

Thanks
Vara

Dora vara

unread,
Apr 15, 2013, 1:49:22 PM4/15/13
to webd...@googlegroups.com
Hi Mahesh,

Thanks for your reply. I have attached my code. If you have any examples, please share it. It would helpful for me to proceed. 
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
public static void main(String[] args) throws AWTException, InterruptedException, MalformedURLException{
WebDriver driver;
driver = new FirefoxDriver();
String baseUrl = "http://gmail.com";
driver.get(baseUrl + "/");
driver.findElement(By.id("Email")).sendKeys("User1");
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password1");
driver.findElement(By.id("signIn")).submit();
String parentWindowHandl = driver.getWindowHandle();
System.out.println("Parent " +parentWindowHandl);
driver.findElement(By.xpath("//*[@id='gb_119']/span[2]")).click();
Thread.sleep(8000);
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);

}
Locatable hoverIte = (Locatable) driver.findElement(By.xpath("//*[@id='contentPane']/div/div[2]/div[1]/div[1]/div/div[8]/div[2]/span[1]"));

driver.findElement(By.xpath("//*[@id='contentPane']/div/div[2]/div[1]/div[1]/div/div[8]/div[2]/span[1]")).click();
Thread.sleep(5000);
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverIte.getCoordinates());
Thread.sleep(7000);
WebElement ai = driver.findElement(By.xpath("//div[@class='Ji rc dba']"));
ai.click();


//File file=new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"); URL url=file.toURL();

setClipboardData("C:\\home\\Desert.jpg");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.id("gbi4t")).click();
driver.findElement(By.id("gb_71")).click();

}

}

Thanks
vara

darrell

unread,
Apr 15, 2013, 10:25:48 PM4/15/13
to webd...@googlegroups.com
Selenium is using javascript to drive the browser. Javascript exists in a sandbox; it does not have access to the native file system. If javascript could access the native file system, it opens the average web browser user to virus attacks. Bottom line, when you click the button to ' browse' the file system for a given file, the dialog acts like a big black hole to Selenium (javascript). It waits for the user to browse to a file and click Open. The dialog then returns the name of the file selected.

On the applications I have worked on the browse file capability is a <FORM> which has a button to pop open the file select dialog. When the file dialog returns, the web form places the filename in a hidden <INPUT> tag of type "file". So the file dialog is really just to populate a hidden field. Submitting the form uses the hidden input to get the filename.

What I have done is use the Selenium Javascript Executor to change the hidden input dialog to a visible input dialog. Then I use sendKeys to place the name of the file in the input. I completely skip past the file dialog by supplying the filename myself. While the automation is running the magically appearing input messes up the formatting but if I'm not testing the formatting it works for what I want. I don't see the need for using the file dialog because it is part of the browser and not part of the code I am testing.

Darrell

neeraj bakhtani

unread,
Sep 25, 2013, 5:05:42 AM9/25/13
to webd...@googlegroups.com
Hi,
Please find the Below link
http://bugreaper.blogspot.in/2013/09/upload-file-using-autoitwebdriver.html

I hope it solves your query,
I u like the post, comment and Join this site 
Thanks in advance

Avinoam Ashkenazi

unread,
May 4, 2015, 8:04:06 PM5/4/15
to webd...@googlegroups.com
the link is broken. the blog doesn't exist.

Mahesh Mahe

unread,
Mar 6, 2017, 3:05:50 AM3/6/17
to webdriver
I have some file or image in folder those images are downloaded form selenium java script, in that site image may be 1 or 2 or 3 or 4 or 5 etc... i have gave instruction to download but for attachment i need to upload image whatever the images are downloaded. i have given to upload 5 images when 5 images are there in folder there is not problem but when i upload less than 5 remaining is showing black.

Note:- if image doesn't exist in the folder i need to skip the step please help me. if you found any solution send the code at mahes...@gmail.com
Thank you

I'm using below code

//upload file

WebElement element1= driver2.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div[2]/form/div[1]/div[2]/div/div[2]/ul/li[1]/input"));

element1.sendKeys("/Users/admin/Desktop/NewFolder/463122831.jpg");

Thread.sleep(1000);

WebElement element2= driver2.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div[2]/form/div[1]/div[2]/div/div[2]/ul/li[2]/input"));

element2.sendKeys("/Users/admin/Desktop/NewFolder/463122832.jpg");

Thread.sleep(1000);

WebElement element3= driver2.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div[2]/form/div[1]/div[2]/div/div[2]/ul/li[3]/input"));

element3.sendKeys("/Users/admin/Desktop/NewFolder/463122833.jpg");

Thread.sleep(1000);

WebElement element4= driver2.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div[2]/form/div[1]/div[2]/div/div[2]/ul/li[4]/input"));

element4.sendKeys("/Users/admin/Desktop/NewFolder/463122834.jpg");

Thread.sleep(1000);

WebElement element5= driver2.findElement(By.xpath("/html/body/div[4]/div[2]/div[2]/div[2]/form/div[1]/div[2]/div/div[2]/ul/li[5]/input"));

element5.sendKeys("/Users/admin/Desktop/NewFolder/463122835.jpg");

Reply all
Reply to author
Forward
0 new messages