首页 > 解决方案 > 在 Selenium WebDriver 中捕获上传文件大小

问题描述

如果我想在使用 Selenium WebDriver 上传文件后捕获文件大小,是否有可能?顺便说一句,我正在使用 Java。目前我正在关注这个教程&这里是代码:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver; 

public class upload {

   public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","webdriver location");
       String baseUrl = "http://demo.guru99.com/test/upload/";
       WebDriver driver = new ChromeDriver();

       driver.get(baseUrl);
       WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));

       // enter the file path onto the file-selection input field
       uploadElement.sendKeys("file location");

       // check the "I accept the terms of service" check box
       driver.findElement(By.id("terms")).click();

       // click the "UploadFile" button
       driver.findElement(By.name("send")).click();
       }
}

我想在控制台中打印文件大小输出。我可以知道这个动作是否可行?谢谢。

标签: javaseleniumselenium-webdriverautomated-testsselenium-chromedriver

解决方案


你的意思是这样的吗?它在上传之前打印上传文件的大小。

import java.io.File;
String fileName = "C:/users/something.txt";
File f = new File(fileName);
long fileSize = f.length();
System.out.format("The size of the file: %d bytes", fileSize);

推荐阅读