如何在使用 Selenium 上传之前获取文件的大小

how do I get the size of the file before upload using Selenium

我已经尝试了好几天了..

我只想在上传文件之前获取文件的大小。

像这样:

IWebElement element = driver.FindElement(By.Id("FileUpload"));
string fileSizeInKB = element.GetAttribute("files[0].size");

或者这个:

IWebElement element = driver.FindElement(By.Id("FileUpload"));
string fileSizeInKB = element.GetAttribute("files")[0].size;

这两种方法都行不通,所以我想问一下如何归档类似的东西?

您可以使用 System.IO.File 命名空间

byte[] buffer = System.IO.File.ReadAllBytes(@"yourfilename");
int size = buffer.Length;

更新

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("Your JavaScript Here");