上传控件
''' selenium无法直接操作windows窗体,上传控件有属性(type="file")时,可直接sendkeys处理上传文件 WebElement adFileUpload = driver.findElement(By.id(" ###")); // 定位上传控件 String filePath = "C:\\test\\uploadfile \\test.jpg"; // 定义了一个本地文件的路径 adFileUpload.sendKeys(filePath); // 为上传控件进行赋值,将需要上传的文件路径赋给控件 ''' def upload_picture(by,locator,value): try: element = get_element(driver, by, locator) path = r"D:\installs\images\%s.png"%(value) element.send_keys(path) except Exception as e: print(e) #实现非input标签上传文件,调用此方法需要打开windows上传文件的系统窗口再调用 def upload_file(path): try: #file_path:上传文件的路径 dialog = win32gui.FindWindow("#32770", "打开") comboxex32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None) combox = win32gui.FindWindowEx(comboxex32, 0, "ComboBox", None) edit = win32gui.FindWindowEx(combox, 0, "Edit", None) button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&0)") win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, path) win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) except Exception as e: print(e)
心之所向无人可挡