Python + Selenium automated simulation mouse
In webdriver, some operations, such as a mouse: Double, right-click, hover, drag and so is encapsulated in ActionChains class, we only need to use when introducing the class on it.
Mouse class provides a common method 0.ActionChains:
- perform (): execution behavior of all ActionChains stored.
- context_click (): Right-click
- double_click (): double-click
- drag_and_drop (): Drag
- move_to_element (): Hover
note:
- Need to introduce ActionChains class before use.
from selenium.webdriver.common.action_chains import ActionChains
Right-click examples
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains # 引入 ActionChains 类
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
# 定位到要右击的元素
right_click = browser.find_element_by_link_text('新闻')
# 对定位到的元素执行鼠标右键操作
#ActionChains(driver):调用ActionChains()类,并将浏览器驱动browser作为参数传入
#context_click(right_click):模拟鼠标双击,需要传入指定元素定位作为参数
#perform():执行ActionChains()中储存的所有操作,可以看做是执行之前一系列的操作
try:
ActionChains(browser).context_click(right_click).perform()
print('成功右击')
except Exception as e:
print('fail')
#输出内容:成功双击
note:
- ActionChains (driver): call ActionChains () class, browser and browser driver passed as a parameter
- context_click (right_click): Analog and double click need to pass as a parameter to specify the positioning element
- perform (): execution ActionChains () stored in all operations, can be seen as a series of operations performed before
1. Right-click
- context_click (): Right-click
# 鼠标右击
# 定位到要右击的元素
right_click = browser.find_element_by_id("xx")
# 对定位到的元素执行右击操作
ActionChains(browser).move_to_element(right_click ).perform()
2. Double-click the Mouse
- double_click (): double-click
# 定位到要右击的元素
double_click = browser.find_element_by_id('xx')
# 对定位到的元素执行鼠标右键操作
ActionChains(browser).context_click(double_click).perform()
3. Drag the mouse
- drag_and_drop(source,target):拖动
- source: starting position; need to drag the elements
- target: end position; Drag need to place a destination element
# 开始位置:定位到元素的原位置
source = driver.find_element_by_id("xx")
# 结束位置:定位到元素要移动到的目标位置
target = driver.find_element_by_id("xx")
# 执行元素的拖放操作
ActionChains(driver).drag_and_drop(source,target).perform()
4. Hover
- move_to_element (): Hover
# 定位到要悬停的元素
move = driver.find_element_by_id("xx")
# 对定位到的元素执行悬停操作
ActionChains(driver).move_to_element(move).perform()
In webdriver, some operations, such as a mouse: Double, right-click, hover, drag and so is encapsulated in ActionChains class, we only need to use when introducing the class on it.
Mouse class provides a common method 0.ActionChains:
- perform (): execution behavior of all ActionChains stored.
- context_click (): Right-click
- double_click (): double-click
- drag_and_drop (): Drag
- move_to_element (): Hover
note:
- Need to introduce ActionChains class before use.
from selenium.webdriver.common.action_chains import ActionChains
Right-click examples
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains # 引入 ActionChains 类
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
# 定位到要右击的元素
right_click = browser.find_element_by_link_text('新闻')
# 对定位到的元素执行鼠标右键操作
#ActionChains(driver):调用ActionChains()类,并将浏览器驱动browser作为参数传入
#context_click(right_click):模拟鼠标双击,需要传入指定元素定位作为参数
#perform():执行ActionChains()中储存的所有操作,可以看做是执行之前一系列的操作
try:
ActionChains(browser).context_click(right_click).perform()
print('成功右击')
except Exception as e:
print('fail')
#输出内容:成功双击
note:
- ActionChains (driver): call ActionChains () class, browser and browser driver passed as a parameter
- context_click (right_click): Analog and double click need to pass as a parameter to specify the positioning element
- perform (): execution ActionChains () stored in all operations, can be seen as a series of operations performed before
1. Right-click
- context_click (): Right-click
# 鼠标右击
# 定位到要右击的元素
right_click = browser.find_element_by_id("xx")
# 对定位到的元素执行右击操作
ActionChains(browser).move_to_element(right_click ).perform()
2. Double-click the Mouse
- double_click (): double-click
# 定位到要右击的元素
double_click = browser.find_element_by_id('xx')
# 对定位到的元素执行鼠标右键操作
ActionChains(browser).context_click(double_click).perform()
3. Drag the mouse
- drag_and_drop(source,target):拖动
- source: starting position; need to drag the elements
- target: end position; Drag need to place a destination element
# 开始位置:定位到元素的原位置
source = driver.find_element_by_id("xx")
# 结束位置:定位到元素要移动到的目标位置
target = driver.find_element_by_id("xx")
# 执行元素的拖放操作
ActionChains(driver).drag_and_drop(source,target).perform()
4. Hover
- move_to_element (): Hover
# 定位到要悬停的元素
move = driver.find_element_by_id("xx")
# 对定位到的元素执行悬停操作
ActionChains(driver).move_to_element(move).perform()