工具:winspy
Python所需安装模块:pywin32 (安装语句:pip install -i https://pypi.douban.com/simple pywin32)
元素位置:
对应代码:
from selenium import webdriver from time import sleep import win32gui import win32con # 上传文件 upload = browser.find_element_by_xpath("//button[@class='el-button specialBtn el-button--default']//span") upload.click() sleep(1) # 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,'E:jmeter-testcase.csv') # 往输入框输入绝对地址 win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button) # 按button sleep(5)
备注:此代码仅适用于Chrome浏览器,Firefox及IE浏览器可能title(“打开”)会不一样,后续会根据浏览器去优化代码。
参考文章:https://www.cnblogs.com/xiaogongjin/p/11546518.html