如何在沒有文字框的情況下在 Selenium 中上傳檔案?
我們可以在沒有文字框的情況下在 Selenium 中上傳檔案。這是透過 sendKeys 方法實現的。它應用於執行選擇要上傳的檔案路徑的任務的 Web 元素上。

當我們嘗試上傳時,我們將單擊瀏覽按鈕。如果我們調查其 HTML 程式碼,我們將能夠找到值 file 的屬性 type。此外,要上傳的檔案路徑應準確。

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

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