selenium上传图片或者文件(可随机上传文件)

import win32con
import win32gui
def img(path, list_name):  # 随机返回目标文件下的图片
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            img(file_path, list_name)
        else:
            list_name.append(file_path)
    l1 = random.choice(list_name)
    return (l1)

调用
	path = 'C:\\Users\\Qin\\Desktop\\星优选测试图片\\正式'
    list_name = []
    # --------win32gui上传文件
    dialog = win32gui.FindWindow( '#32770', u'打开' )  # 对话框
    ComboBoxEx32 = win32gui.FindWindowEx( dialog, 0, 'ComboBoxEx32', None )
    ComboBox = win32gui.FindWindowEx( ComboBoxEx32, 0, 'ComboBox', None )
    Edit = win32gui.FindWindowEx( ComboBox, 0, 'Edit', None )  # 上面三句依次寻找对象,直到找到输入框Edit对象的句柄
    button = win32gui.FindWindowEx( dialog, 0, 'Button', None )  # 确定按钮Button
    win32gui.SendMessage( Edit, win32con.WM_SETTEXT, None,  img(path, list_name))  # 往输入框输入绝对地址
    time.sleep(1)
    win32gui.SendMessage( dialog, win32con.WM_COMMAND, 1, button )  # 按button



    原文作者:这起个名真难
    原文地址: https://blog.csdn.net/qq_41644139/article/details/122808059
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞