如何使用 Selenium WebDriver 处理 Windows 文件上传?
我们可以使用 Selenium Webdriver 来处理 Windows 文件上传。可通过 sendKeys 方法实现此操作。我们必须首先通过指定要上传的文件路径来识别执行文件选择的元素。
这仅应用于具有 type 属性设置为 file 作为值以及元素标签名称为 input 的元素。以下 HTML 代码显示具有 type = file 值设置的元素。

示例
代码实现
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class WndsFileUpl{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/selenium/selenium_automation_practice.htm");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// identify element
WebElement m=driver.findElement(By.xpath("//input[@type='file']"));
// windows file upload with file path
m.sendKeys("C:\Users\Pictures\Logo.jpg");
driver.close();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP