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!

Anna
  • 137
  • 2
  • 6
  • 15
  • There should be an `input` element responsible for the upload field. – alecxe Feb 23 '15 at 16:47
  • I don't really know what you mean, in this example is like I did it: [example](http://stackoverflow.com/questions/9431978/one-solution-for-file-upload-using-selenium-webdriver-with-java) – Anna Feb 23 '15 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. – alecxe Feb 23 '15 at 16:53
  • I know what you mean.. I change it, now I dont get any error but it does not load the image – Anna Feb 24 '15 at 08:43
  • show part of code that U want to upload file. – SkorpEN Feb 24 '15 at 09:04
  • Did U read Webdriver faq about file upload ??? – SkorpEN Feb 25 '15 at 13:20

1 Answers1

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");
Juhi Saxena
  • 1,197
  • 11
  • 17