[英]Set value of file input to Base64 encoded image
I want my service to test a certain file input by uploading Base64 encoded image.我希望我的服务通过上传 Base64 编码图像来测试某个文件输入。 Is this possible with Selenium? Selenium 可以做到这一点吗?
Lets say i have:可以说我有:
<input type="file">
I know that for regular file upload, i have to set file path for this input, but what about Base64 encoded images?我知道对于常规文件上传,我必须为此输入设置文件路径,但是 Base64 编码图像呢?
Edit: Language is Python编辑:语言是 Python
You haven't specified a language.您尚未指定语言。 This answer is in Java.这个答案在 Java 中。
Encode the image to base64 and upload it.将图片编码为base64并上传。
For file reading, you have to add "Apache Commons IO" libraries.对于文件读取,您必须添加“Apache Commons IO”库。
String filePath = "F:\\cat.jpg";
// Encode image
byte[] fileContent = new byte[0];
try {
fileContent = FileUtils.readFileToByteArray(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
String encodedString = Base64.getEncoder().encodeToString(fileContent);
// Upload image
WebElement uploadElement = driver.findElement(By.id("upload_element_id"));
uploadElement.sendKeys(encodedString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.