0

I have tried to upload a photo using the code

driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");

But the image is not getting uploaded.

The html of the upload button is

<button id="upfile1" class="buttonclass" style="cursor: pointer" type="button"> Choose Photo</button>

Is there any other way to upload image. I have tried using WebElement also. I need a solution in JAVA.

3
  • When you click a button, you will see a pop up box where you need to enter the image path. Click on the button and use Windows send keys to the pop up box. Commented Aug 1, 2014 at 7:40
  • ya when we click on that a pop up appears but its a different window right so how to sendkeys to that window/pop up? can u plz explain? Commented Aug 1, 2014 at 7:41
  • Is there any <input> element present on the page to upload the image? Commented Aug 1, 2014 at 7:43

4 Answers 4

1

I was able to do it using

driver.findElement(By.id("upfile1")).click();
    Thread.sleep(2000);
    StringSelection ss = new StringSelection("C:\\Users\\logo1.jpg");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

But is there any other simple methods to achieve the same other than using robot?

Sign up to request clarification or add additional context in comments.

Comments

0

Add System.Windows.Forms reference and try the below code.

driver.findElement(By.Id("upfile1")).Click();
System.Windows.Forms.SendKeys.SendWait("C:\\Users\\path\\ben.jpg");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");

5 Comments

This shows an error in eclipse as System.Windows cannot be resolved to a type.
The above code should work with C#. There might be other solutions in Java then.
Did you try Action class something like... new OpenQA.Selenium.Interactions.Actions(ie).SendKeys("").Perform();
I have tried using submit() but it is also not uploading the image.
Submit() can't be used for this kind, it is for Form submission only.
0

The pop-up which appears is a windows one.It should be handled using AutoIT.

2 Comments

But I am trying to automate the process with selenium.
@Dinu - Yes but you can integrate AutoIT code in the selenium code or you can download and add the AutoIT jar in your libraries and use the AutoIT code.
0

If you are using RemoteWebDriver, then you need to use LocalFileDector to upload the file to the remote selenium server first. And then use the remote path to upload from the remote selenium server.

driver.setFileDetector(new LocalFileDetector());
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.