简体   繁体   English

使用 selenium webdriver 上传文件

[英]file upload using selenium webdriver

How can we upload a jpeg on UI via automation?我们如何通过自动化在 UI 上上传 jpeg?

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我期待文件被上传,但我收到以下错误 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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