Python Selenium 键盘和鼠标操作
2015-11-10 14:02
781 查看
原文章地址:
http://blog.sina.com.cn/s/blog_620987bf0102v2r8.html
Python也可以模拟鼠标和键盘的操作,不过要注意的是键盘带来的屏幕游标位置的挪动和鼠标在屏幕上的挪动位置,两个是不同的。
首先要在文件头引入
from selenium.webdriver.common.action_chains import ActionChains
#定义一个函数
def Transfer_Clicks(browser):
browser.execute_script("window.scrollBy(0,-document.body.scrollHeight)","")
#这个是执行一段Javascript函数,将网页滚到到网页顶部。
try:
inputs1 = browser.find_elements_by_class_name("feedAttr_transfer")
for input1 in inputs1:
try:
ActionChains(browser).click(input1).perform()
#模拟鼠标点击控件input1,此时的鼠标位置在input1处
browser.execute_script("window.scrollBy(0,200)","")
#向下滚动200个像素,鼠标位置也跟着变了
ActionChains(browser).move_by_offset(0,-80).perform()
#向上移动鼠标80个像素,水平方向不同
ActionChains(browser).click().perform()
#鼠标左键点击
ActionChains(browser).key_down(Keys.TAB).perform()
#模拟tab键的输入
ActionChains(browser).send_keys(Keys.ENTER).perform()
#模拟输入ENTER键
except:
pass
except:
pass
return "Transfer successfully \n"
http://blog.sina.com.cn/s/blog_620987bf0102v2r8.html
Python也可以模拟鼠标和键盘的操作,不过要注意的是键盘带来的屏幕游标位置的挪动和鼠标在屏幕上的挪动位置,两个是不同的。
首先要在文件头引入
from selenium.webdriver.common.action_chains import ActionChains
#定义一个函数
def Transfer_Clicks(browser):
browser.execute_script("window.scrollBy(0,-document.body.scrollHeight)","")
#这个是执行一段Javascript函数,将网页滚到到网页顶部。
try:
inputs1 = browser.find_elements_by_class_name("feedAttr_transfer")
for input1 in inputs1:
try:
ActionChains(browser).click(input1).perform()
#模拟鼠标点击控件input1,此时的鼠标位置在input1处
browser.execute_script("window.scrollBy(0,200)","")
#向下滚动200个像素,鼠标位置也跟着变了
ActionChains(browser).move_by_offset(0,-80).perform()
#向上移动鼠标80个像素,水平方向不同
ActionChains(browser).click().perform()
#鼠标左键点击
ActionChains(browser).key_down(Keys.TAB).perform()
#模拟tab键的输入
ActionChains(browser).send_keys(Keys.ENTER).perform()
#模拟输入ENTER键
except:
pass
except:
pass
return "Transfer successfully \n"
相关文章推荐
- [LeetCode]题解(python):045-Jump Game II
- Python核心数据类型——布尔型
- Python核心数据类型——集合(Set)
- (转)Python 字典排序
- python数据分析实战之泰坦尼克号统计
- Python 定时执行线程 执行任务
- python控制台中实现进度条功能
- python学习问题之-编码
- Python - 开发环境Vim设置
- Python - 多进程编程, 不怕GIL
- 【译】TensorFlow Python Docs 之 overview
- Python 3.x标准模块库目录
- 10 种机器学习算法的要点(附 Python 和 R 代码)
- Python+selenium环境安装
- 6.一个python-selenium的实战
- python的复制,深拷贝和浅拷贝的区别
- python常见模块命令(os/sys/platform)
- python之logging模块
- 用Python写了一个数据格式转换小程序
- python处理时间汇总