简体   繁体   中英

file upload using selenium webdriver

How can we upload a jpeg on UI via automation?

At present I have the image placed in my repo under resources and I'm doing the following code

WebElement element = driver.findElement(By.id("mypicId"));
File file = new File(ClassLoader.getSystemResource("test.jpg").toURI());
element.sendKeys(file.getAbsolutePath());

element I'm sending a file to is an

I'm expecting the file to be uploaded but I get the following error java.util.concurrent.ExecutionException: org.openqa.selenium.WebDriverException: invalid argument: File not found: test.jpg

driver.setFileDetector(new LocalFileDetector());

WebElement element = driver.findElement(By.id("mypicId"));
File file = new File(System.getProperty("user.dir") + "/src/java/resources/test.jpg"));
//or


// File file = new File(System.getProperty("user.dir") + "/src/test/resources/test.jpg"));
element.sendKeys(file.getAbsolutePath());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM