如何使用 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.tw/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