我们需要准备一个上传文件的html,创建a.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>文件上传示例</title>
</head>
<body><form action="/upload" method="post" enctype="multipart/form-data"><!-- 文件上传控件 --><label for="fileUpload">选择文件:</label><input type="file" id="fileUpload" name="file"><!-- 提交按钮 --><input type="submit" value="上传">
</form></body>
</html>
selenuim
import timefrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
web_driver=webdriver.Firefox()
web_driver.get("file:///C:/Users/Administrator/Desktop/a.html")el=(By.ID,"fileUpload")WebDriverWait(web_driver,10).until(EC.presence_of_element_located(el))web_driver.find_element(*el).send_keys(r"C:\Users\Administrator\Desktop\1.jpg")time.sleep(5)web_driver.quit()
drissionpage
import time
from DrissionPage import WebPageweb_page=WebPage("d")
web_page.get("file:///C:/Users/Administrator/Desktop/a.html")el=(By.ID,"fileUpload")web_page.wait.eles_loaded(el)
web_page.ele(el).click.to_upload(r"C:\Users\Administrator\Desktop\1.jpg")time.sleep(5)web_page.quit()