python簡單的系統元件間上傳檔案的操作(適用非input元素)
阿新 • • 發佈:2021-11-18
在用pyautogui上傳檔案的坑:
- 為了讓頁面彈出上傳圖片的對話方塊,小編用了各種方法都失敗(比如用click點選,鍵盤點選,execute_script方式js程式碼等)。
- 定位元素點選click方法沒有效果,換用滑鼠操作點選後成功;廢話不多說,直接上程式碼:
點選檢視程式碼
import time import pyautogui as ui from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() # 隱式等待 driver.implicitly_wait(20) # 進入被測網址 driver.get("file:///C:/Users/test/Desktop/page.html") # 放大視窗 driver.maximize_window() # 強制等待 time.sleep(0.5) # 定位元素,並用滑鼠點選操作 file_elem_2 = driver.find_element(By.XPATH, '//input[@name="s_file"]') ActionChainsDriver = ActionChains(driver).click(file_elem_2).perform() # 系統之間要等待 time.sleep(3) # 寫入檔案 ui.write(r"C:\Users\test\Desktop\api.xlsx") time.sleep(3) # 輸入回車 ui.press('enter', presses=2) time.sleep(2)