如何使用 Selenium Webdriver 上传文件?
我们可以使用 Selenium Webdriver 上传文件。这通过 sendKeys 方法实现。我们首先必须通过提及文件路径(要上传的文件)来识别执行文件选择的元素。
这仅应用于元素的 类型属性设置为文件作为值,同时元素标记名称为输入。以下 html 代码显示了类型 = 文件值设置的元素。

示例
代码实现。
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();
//implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//launch application
driver.get("https://tutorialspoint.com/selenium/selenium_automation_practice.htm");
// identify element
WebElement m=driver.findElement(By.xpath("//input[@type='file']"));
// windows file upload with file path
m.sendKeys("C:\Users\Pictures\Logo.jpg");
//browser close
driver.close();
}
}输出

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