selenium、pywinauto自动化云端https\http文件如何上传到系统中
背景:
平常在工作中公司一般服务器都是云,那么在云端肯定会存储一些例如:PDF、JPG、Excel、Word等文件,那如何通过操作selenium把文件上传到想要的地方呢?一般情况下可能是通过Request请求把文件先下载下来存到固定目录然后再通过selenium读取绝对路径到input标签中去,那问题来了,如果有一些老的input标签无法不上呢?
下面就用pywinauto模拟鼠标点击输入时间来操作上传文件。下面代码kl中的传参 就是http/https请求方式的文件
print_control_identifiers() 可以打印元素定位值
# windows选择文件弹窗
def shangchuan_win_file(kl):
# sleep(2)
app = pywinauto.Desktop()
da = app['文件上传']
da.wait('enabled', timeout=30)
# app['文件上传'].print_control_identifiers()
da['Edit'].wait('enabled', timeout=30)
da['Edit'].type_keys(kl)
sleep(2)
da["打开(&O)"].wait('enabled', timeout=30) # .click_input()
da["打开(&O)"].click_input()
# 有的时候输入文件路径后点击“打开”click_input 事件不好使 所以加了一层判断,循环10次存在的时候点击,否则检查时候还有链接有则关闭
# 判断窗户是否存在 True False
for i in range(0, 10):
if da.exists():
da["打开(&O)"].wait('enabled', timeout=30) # .click_input()
da["打开(&O)"].click_input()
sleep(2)
pass
else:
break
else:
acc = Application().connect(title_re='文件上传')
acc.window_(found_index=0).close()