import win32guiimport win32confrom selenium import webdriverimport timedef main(): driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(10) driver.get("http://www.baidu.com") driver.find_element_by_css_selector(".soutu-btn").click() str_js = 'document.getElementsByClassName("upload-pic")[0].click()' driver.execute_script(str_js) # 一定要等待windows窗口打开,再用pywin32api操作窗口 time.sleep(5) # 一级顶层窗口,此处title为上传窗口名称,浏览器不一样上传窗口名称不一样 dialog_1 = win32gui.FindWindow("#32770", "打开") # 二级窗口 dialog_2 = win32gui.FindWindowEx(dialog_1, 0, "ComboBoxEx32", None) # 三级窗口 dialog_3 = win32gui.FindWindowEx(dialog_2, 0, "ComboBox", None) # 文本框 edit = win32gui.FindWindowEx(dialog_3, 0, 'Edit', None) # 按钮 button = win32gui.FindWindowEx(dialog_1, 0, 'Button', None) # 需要上传的图片 file_path = "D:\\abc.jpg" # 输入文件路径 win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path) # 点击按钮 win32gui.SendMessage(dialog_1, win32con.WM_COMMAND, 1, button) time.sleep(10) driver.close() driver.quit()if __name__ == '__main__': main()
下面截图是winspy64工具的使用,,获取windows窗口的class和title
按住拖动,要获取信息的窗口,点击Tree按钮即可,获取到当前窗口和子级.依次获取需要的信息
来源:https://www.cnblogs.com/wgy2020/p/12151063.html