繁体   English   中英

将文件输入的值设置为 Base64 编码图像

[英]Set value of file input to Base64 encoded image

我希望我的服务通过上传 Base64 编码图像来测试某个文件输入。 Selenium 可以做到这一点吗?

可以说我有:

<input type="file">

我知道对于常规文件上传,我必须为此输入设置文件路径,但是 Base64 编码图像呢?

编辑:语言是 Python

您尚未指定语言。 这个答案在 Java 中。

将图片编码为base64并上传。

对于文件读取,您必须添加“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.

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