Anna
Anna

Reputation: 137

Test Automation. File upload Java and Selenium

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!

Upvotes: 0

Views: 363

Answers (1)

Juhi Saxena
Juhi Saxena

Reputation: 1217

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");

Upvotes: 1

Related Questions