python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
来源:互联网 发布:淘宝简易床 编辑:程序博客网 时间:2024/05/16 11:32
Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框
这种情况,Webdriver暂时是不支持除页面外的其他操作
前期尝试过很多种方法,比如send_keys("path"),但是都无法解决
虽然Webdriver无法对底层进行操作,但是Python的扩展是可以的
通过模拟鼠标、键盘的操作,可以解决这个问题,但是这个方法比较笨,而且不怎么理想,如果有好的方法,请留言给我,谢谢!
解决方法:
1、扩展python包
对底层进行操作,需要导入win32api,win32con
直接把两个模块下载下来进行安装即可
2、在python中进行调用实现鼠标定位,点击、键盘输入的操作
PS:在对文件选择弹出框进行定位,点击的时候,在我的操作系统下,出现了无法点击的情况,但在同事的电脑上却可以操作,于是我采取了模拟键盘输入文件名,确保文件能够正确上传
time的作用很重要,起到一个缓冲的作用,如果加载速度慢,没有加time,可能导致这种方法失败
①模拟键盘输入(要注意文件的位置)
import win32conimport win32apifrom ctypes import *import timedef mouse_click(x, y): # 设置鼠标初始坐 windll.user32.SetCursorPos(x, y) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) # 鼠标左键单击 time.sleep(1) win32api.keybd_event(65,0,0,0) #a键位码是97 win32api.keybd_event(65,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(65,0,0,0) #a键位码是97 win32api.keybd_event(65,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(110,0,0,0) #.键位码是46 win32api.keybd_event(110,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(74,0,0,0) #j键位码是106 win32api.keybd_event(74,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(80,0,0,0) #p键位码是112 win32api.keybd_event(80,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(71,0,0,0) #g键位码是103 win32api.keybd_event(71,0,win32con.KEYEVENTF_KEYUP,0) time.sleep(1) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) # 鼠标左键释放 time.sleep(1) win32api.keybd_event(13,0,0,0) #enter键位码 win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0) win32api.keybd_event(13,0,0,0) #enter键位码 win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0) time.sleep(1)
②模拟鼠标定位选择文件
1
2
3
4
5
6
def
mouse_double_click(x,y):
windll.user32.SetCursorPos(x,y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,
0
,
0
,
0
,
0
)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,
0
,
0
,
0
,
0
)
# 鼠标左键释放
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,
0
,
0
,
0
,
0
)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,
0
,
0
,
0
,
0
)
# 鼠标左键释放
其实两种方法的都差不多,第二种方法实际是双击的效果
0 0
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
- selenium webdriver 模拟鼠标键盘的操作
- selenium-webdriver(python)定位问题
- selenium webdriver 模拟键盘操作
- python模拟键盘鼠标操作
- webdriver for python模拟键盘操作
- Python Selenium 键盘和鼠标操作
- python selenium鼠标键盘操作(ActionChains)
- selenium+python键盘加鼠标操作
- selenium-webdriver(python) -- 鼠标事件
- selenium+Python学习之调用webdriver接口方法来打开和关闭Chrome浏览器
- selenium + python 通过cookie来实现登录
- qtp录制鼠标右键单击事件要通过模拟键盘操作来实现
- 使用Python来模拟鼠标的点击
- 使用Python来模拟鼠标的点击
- python 捕捉和模拟鼠标键盘操作
- 通过切换iframe来定位元素(用于Python+selenium自动化测试)
- selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
- Java 数字格式化 DecimalFormat
- [Leetcode]Guess Number Higher or Lower II
- Effective Java 学习笔记(二)
- OSI参考模型
- 19. Remove Nth Node From End of List
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
- Docker 简介
- arm+linux fl2440 ds18b20 温度传感器驱动编写及测试
- iScroll.js 用法参考
- 为什么改变字符串中的字符需要使用引用
- Java代码的规范(自己在公司实习的过程自己总结的代码规范,不断更新)
- Java学习日志(1)
- iOS学习笔记40-日志重定向
- iOS-Core-Animation-Advanced-Techniques(五)