Uploading images in Selenium Webdriver ?? Help me out

186 views
Skip to first unread message

Sudheer Reddy

unread,
Oct 23, 2013, 5:22:21 AM10/23/13
to seleniu...@googlegroups.com
Hi All, 

How to upload images in selenium webdriver? I have been googling for this. I found like we can do this by using AutoIt.

Can't we upload images with out using Autoit ?? I don't want to use this.

In my application I will click on a button called Upload. Then a window appears then I have to select the images.

I have used this  driver.findElement(By.xpath("xpath of upload btn")).sendKeys("i have given here image path in my system");

but this doesn't works for me. After clicking on upload window is opening. But images are not getting selected.

Can anyone help me out??

Thanks in advance.,

Sudheer

venkat paloju

unread,
Oct 23, 2013, 6:31:24 AM10/23/13
to seleniu...@googlegroups.com
Hi Sudheer,

We can automate this using Java Robort frame work, Please find the code as follows,

public void enterFilePathInBrowseWindow(String filePath)
{
Robot copyRobot, submitRobot ;
Clipboard clipboard ;
StringSelection stringSelection ;
  
try {
copyRobot = new Robot();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   stringSelection = new StringSelection( filePath );   
   clipboard.setContents(stringSelection, null);
   
   copyRobot.delay(1000);
   copyRobot.waitForIdle();
  
   copyRobot.keyPress(KeyEvent.VK_CONTROL);
   copyRobot.keyPress(KeyEvent.VK_V);    
   copyRobot.keyRelease(KeyEvent.VK_V);
   copyRobot.keyRelease(KeyEvent.VK_CONTROL);
   
   
   
   submitRobot = new Robot();
 
   submitRobot.keyPress(KeyEvent.VK_ENTER);
   submitRobot.keyPress(KeyEvent.VK_ENTER);
   submitRobot.keyRelease(KeyEvent.VK_ENTER);
   copyRobot.delay(500);
   
   }
   catch (AWTException ex)
   {
    ex.getMessage();
   }
}

Try this by passing upload location.
Thanks,
Venkat.

kriti....@gmail.com

unread,
Oct 23, 2013, 8:32:40 AM10/23/13
to seleniu...@googlegroups.com
     
         Hi Sudheer,
        
           //try out this with out using Autoit
           driver.get(<page url>);
           driver.findElement(By.xpath("<link if any available to open the pop-up window to upload file")).click();
           driver.findElement(By.xpath(".//*[@id='file']")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\images112.jpg");
           driver.findElement(By.xpath("<xpath of button>")).click();
           driver.get("page url>") --just to refresh the page post uploading done.

    Thanx,
    kriti
Reply all
Reply to author
Forward
0 new messages