Selenium webdriver 如何向瀏覽器上傳檔案?
藉助 Selenium webdriver,我們可以向瀏覽器上傳檔案。透過對元素使用 sendKeys() 方法來實現,透過指定要上傳的檔案的路徑,該方法執行檔案的選擇。

在使用檔案上傳功能時,我們需要點選“瀏覽”按鈕。當元素的屬性型別值為檔案時,webdriver 會處理這一步。另外,要上傳的檔案的路徑必須正確。

示例
程式碼實現。
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 FileUpld{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url ="https://tutorialspoint.tw/selenium/selenium_automation_practice.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify element
WebElement l=driver.findElement(By.xpath("//input[@name='photo']"));
// file selection field with file path
l.sendKeys("C:\Users\ghs6kor\Pictures\Tulips.jpg");
driver.quit();
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP