0

I am trying to upload a file but no succes, I found this example:

WebElement element = getSupport().getDriver().findElement(By.xpath(".//input[@type='file']"));
element.sendKeys("D:/Profiles/user/workspace/copla-selenium/src/test/resources/datasets/default/test-image.jpg");

But I get this error:

2015-02-23 17:32:59 ERROR root:97 - Test failed org.openqa.selenium.WebDriverException: unknown error: cannot focus element

Any idea? Thanks!

6
  • There should be an input element responsible for the upload field. Commented Feb 23, 2015 at 16:47
  • I don't really know what you mean, in this example is like I did it: example Commented Feb 23, 2015 at 16:51
  • 2
    See the xpath used in that thread points to an input element - yours is pointing to an a element. This is probably the problem here. Commented Feb 23, 2015 at 16:53
  • I know what you mean.. I change it, now I dont get any error but it does not load the image Commented Feb 24, 2015 at 8:43
  • show part of code that U want to upload file. Commented Feb 24, 2015 at 9:04

1 Answer 1

1

Are you using webdriver , If yes then sendKeys never works for webdriver. If you want to use webdriver then try autoIt or robot class for file uploading. Otherwise you can use remote webdriver Like this:

RemoteWebDriver rdriver = new RemoteWebDriver(caps);
    rdriver.setFileDetector(new LocalFileDetector());
    WebElement element = rdriver.findElement(By
            .xpath(".//input[@type='file']"));
    element.sendKeys("D:/Profiles/user/workspace/copla-selenium/src/test/resources/datasets/default/test-image.jpg");
Sign up to request clarification or add additional context in comments.

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.