Selenium鼠标模拟事件

from selenium.webdriver.common.action_chains import ActionChains
 
原理:调用ActionChains的方法时不会立即执行,会将所有的操作按顺序存放在一个队列里,当调用perform()方法时,队列中的事件会依次执行
支持链式写法或者分步写法
 
例:
#定位到鼠标移动上边的元素
menu_ele = driver.find_element_by_css_selector('.list_item > li:nth-child(1) > img:nth-child(1)')
ActionChains(driver).move_to_element(menu_ele).perform()
#选中子菜单
sub_menu_ele = driver.find_element_by_css_selector('.base > div:nth-child(3) > a:nth-child(2)')
sleep(3)
sub_menu_ele.click()
 
归纳:
ActionChains(driver).click(ele).perform()
鼠标和键盘方法列表:
perform() 执行链中的所有动作
click(on_element=None) 单击鼠标左键
context_click(on_element=None) 点击鼠标右键
double_click(on_element=None) 双击鼠标左键
move_to_element(to_element) 鼠标移动到某个元素
ele.send_keys(keys_to_send) 发送某个词到当前焦点的元素
========== 不常用 ==========
click_and_hold(on_element=None) 点击鼠标左键,不松开
release(on_element=None) 在某个元素位置松开鼠标左键
key_down(value, element=None) 按下某个键盘上的键
key_up(value, element=None) 松开某个键
drag_and_drop(source, target) 拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) 拖拽到某个坐标然后松开
move_by_offset(xoffset, yoffset) 鼠标从当前位置移动到某个坐标
move_to_element_with_offset(to_element, xoffset, yoffset) 移动到距某个元素(左上角坐标)多少
距离的位置
send_keys_to_element(element, keys_to_send) 发送某个键到指定元素

posted @ 2020-12-06 13:19  如沫  阅读(123)  评论(0编辑  收藏  举报