有 2 种情况:
file_path = "C:\Usersj\Documents\1111.txt"
1.如果标签名是 input,可以直接输入路径,直接调用 send_keys
driver.find_element(By.ID,'aa').send_keys(file_path)
2.非 input 标签的上传,则需要第三方工具
2.1 AutoIt
具体可以参考虫师的博客
; Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",5)
; Set the File name text on the Edit field
controlSetText("打开","","Edit1","C:\Users\hongzj\Pictures\七夕\1.gif")
Sleep(2000)
; Click on the Open button
ControlClick("打开","","Button1");
2.2SendKeys 第三方库(目前只支持到 2.7 版本)
2.3 Python pywin32 库,识别对话框句柄,进而操作
import win32gui
import win32con
def upload_chrome(filepath):
# x 一级窗口
dialog = win32gui.FindWindow("#32770","打开")
# 二级窗门
Comboboxex32 = win32gui.FindWindowEx(dialog, 0, 'Comboboxex32', None)
# 三级窗口
combobox = win32gui.FindWindowEx(Comboboxex32, 0, 'Combobox', None)
# 四级窗口 - 文件路经输入框
edit = win32gui.FindWindowEx(combobox, 0, 'Edit', None)
# 二级窗口一打开按钮
button = win32gui.FindWindowEx(dialog, 0, "Button", "打开 (&0)")
# 操作一 # 发送文件路经
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, filepath)
# 点击打开按钮
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)

指定要上传的文件路径

upload_chrome(file_path)


↙↙↙阅读原文可查看相关链接,并与作者交流