如何使用硒 (Python)上傳檔案?


我們可以使用 Python 透過 Selenium 上傳檔案。這可以透過 send_keys 方法實現。首先,找到選擇要上傳的檔案路徑的元素。

此功能僅適用於具有型別屬性且該屬性設定為檔案型別屬性的元素。此外,該元素的標記名應該是輸入。我們調查一下具有以上屬性的元素的 HTML 程式碼。

示例

程式碼實現。

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.maximize_window()

driver.get("https://tutorialspoint.tw/selenium/selenium_automat
ion_practice.htm")
#to identify element
s = driver.find_element_by_xpath("//input[@type='file']")
#file path specified with send_keys
s.send_keys("C:\Users\Pictures\Logo.jpg")

輸出

更新於:2020 年 12 月 28 日

14K+ 瀏覽

開啟您的職業生涯

完成課程獲取認證

立即開始
廣告