1、安装相关模块
pyperclip: 实现脚本字段的复制、粘贴
安装方式
pip install pyperclip
2、导入相关模块
import win32api
import win32con
import pyperclip
3、封装代码
涉及到模拟按键等操作可参考链接:https://blog.csdn.net/polyhedronx/article/details/81988948.
class BasePage:
...
def isnot_input_uploadfile(self, eleLoc, filePath, doc=''):
"""
使用python的win32api,win32con模拟按键输入,实现文件上传操作
:param eleLoc: 页面中的上传文件按钮
:param filePath: 要上传的文件地址(绝对路径)
:param doc: 备注信息
:return:
"""
pyperclip.copy(filePath)
self.click(eleLoc)
time.sleep(3)
win32api.keybd_event(17, 0, 0, 0)
win32api.keybd_event(86, 0, 0, 0)
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
win32api.keybd_event(13, 0, 0, 0)
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(2)
4、调用函数
def add_datas_zcdj_page(self, zcdjh, zcdjrq, zcdjjg, gldwyj, zgdwsh, zgbmsh, ljpath):
time.sleep(1)
self.switch_iframe_by_id(IMPL.zcdj_add_iframe[0], doc="xxxx新增页面iframe")
self.input_text(IMPL.zcdjh, zcdjh, doc="输入xxxx")
self.input_text(IMPL.zcdjrq, zcdjrq, doc="输入xxxx")
win32api.keybd_event(13, 0, 0, 0)
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.5)
self.input_text(IMPL.zcdjjg, zcdjjg, doc="输入xxxx")
self.input_text(IMPL.gldwyj, gldwyj, doc="输入xxxx")
self.input_text(IMPL.zgdwsh, zgdwsh, doc="输入xxxx")
self.input_text(IMPL.zgbmsh, zgbmsh, doc="输入xxxx")
self.isnot_input_uploadfile(IMPL.fjsc, ljpath, doc="上传文件")
time.sleep(1)
self.click(IMPL.save_btn, doc="点击保存按钮")
self.driver.switch_to_default_content()
def test_f_zcdj_page_add_save1(self):
"""
注册登记新增界面,编辑相关信息后,点击保存
数据新增成功
:return:
"""
IMP(self.driver).add_datas_zcdj_page(
GD.zcdj_add_datas1['ZCDJH'],
GD.zcdj_add_datas1['DJRQ'],
GD.zcdj_add_datas1['DJJG'],
GD.zcdj_add_datas1['GLDWYJ'],
GD.zcdj_add_datas1['ZGDWYJ'],
GD.zcdj_add_datas1['ZGBMYJ'],
GD.zcdj_add_datas1['FJ']
)
time.sleep(1)
class GlobalDatas:
zcdj_add_datas1 = {
"ZCDJH": "xxxx", "DJRQ": "2021-09-17", "DJJG": "xxxx", "GLDWYJ": "同意", "ZGDWYJ": "同意",
"ZGBMYJ": "同意", "FJ": r"E:\Users\PycharmProjects\Auto_Testing_01\Infos\注册登记信息.docx"
}
|