python+selenium模拟鼠标操作
Posted 小白龙白龙马
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+selenium模拟鼠标操作相关的知识,希望对你有一定的参考价值。
from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包
--------------------------------------------------------------------------------------------
submit = driver.find_element_by_id(‘kw‘) #首先创建对象
ActionChains(driver).click(submit).perform() #左键
ActionChains(driver).context_click(submit).perform() #右键
ActionChains(driver).double_click(submit).perform() #双击
----------------------------------------------------------------------------------------------------------------------
ActionChains(driver).drag_and_drop_by_offset(submit,10,10).perform() #拖放到指定坐标位置
#target也是创建的一个对象
ActionChains(driver).drag_and_drop(submit,target).perform() #拖放到目标元素位置
--------------------------------------------------------------------------------------------
ActionChains(driver).move_by_offset(10,10).perform() #鼠标在指定坐标悬停
ActionChains(driver).move_to_element(submit).perform() #鼠标在指定元素悬停
ActionChains(driver).move_to_element_with_offset(submit,5,5).perform() #鼠标在指定元素的指定坐标悬停
---------------------------------------------------------------------------------------------------------
ActionChains(driver).click_and_hold(submit).perform() #鼠标左键元素并保持
ActionChains(driver).context_click(submit).perform() #鼠标右键元素并保持
-----------------------------------------------------------------------------------------------
ActionChains(driver).key_down(Keys.CONTROL).send_keys(‘c‘).key_up(Keys.CONTROL).perform() #ctrl+c 拷贝组合键
================================================================================================
from selenium import webdriver
from selenium.webdriver.common.keys import Keys #导入键盘相关的包
from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包
from time import sleep
driver = webdriver.Firefox() # 指定和打开浏览器
driver.get(‘http://www.baidu.com‘)
#driver.find_element_by_id(‘kw‘).send_keys(‘中国‘)
#sleep(4)
#submit = driver.find_element_by_id(‘su‘)
#ActionChains(driver).click(submit).perform() #对搜索按钮 鼠标左点击
#sleep(4)
#------------------------------------------------------------------------------------------
#submit = driver.find_element_by_link_text("设置")
#ActionChains(driver).move_to_element(submit).perform() #鼠标悬停在上面
#sleep(5)
#driver.find_element_by_class_name("setpref").click() # 打开搜索设置
#sleep(2)
#-----------------------------------------------------------------------------------------
location01 = driver.find_element_by_link_text(‘新闻‘) # 鼠标拖动事件
sleep(7)
location02 = driver.find_element_by_link_text(‘更多产品‘)
ActionChains(driver).drag_and_drop(location01, location02).perform()
sleep(8)
driver.close()
=============================================================================================
from selenium import webdriver
from selenium.webdriver.common.keys import Keys #导入键盘相关的包
from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包
from time import sleep
driver = webdriver.Firefox() # 指定和打开浏览器
driver.get(‘http://www.baidu.com‘)
driver.find_element_by_id(‘kw‘).send_keys(‘中国‘)
sleep(4)
driver.find_element_by_id(‘kw‘).click()
ActionChains(driver).key_down(Keys.CONTROL).send_keys(‘a‘).key_up(Keys.CONTROL).perform() #全选
sleep(4)
ActionChains(driver).key_down(Keys.CONTROL).send_keys(‘c‘).key_up(Keys.CONTROL).perform() #复制、拷贝
sleep(4)
driver.find_element_by_id(‘kw‘).click()
ActionChains(driver).key_down(Keys.CONTROL).send_keys(‘v‘).key_up(Keys.CONTROL).perform() #粘贴
sleep(8)
driver.close()
以上是关于python+selenium模拟鼠标操作的主要内容,如果未能解决你的问题,请参考以下文章
python + selenium 模拟键盘升级版PyUserInput
selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等