簡體   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