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 webdriverfrom 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 webdriverfrom 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()
原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12041758.html