selenium模拟事件处理

执行原理:

调用ActionChains的方法时不会立即执行,会将所有的操作按顺序存放在一个队列里,当调用perform()方法时,从队列中的事件会依次执行。

支持链式写法或者是分布写法。

鼠标键盘方法列表:

    perform() 执行链中的所有动作

    click(on_element=None) 单击鼠标左键

    context_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) 移动到距某个元素(左上角坐标)多少距离的位置

鼠标的事件hover菜单栏弹出

# -*- coding:UTF-8 -*-
__autor__ = 'zhouli'
__date__ = '2018/11/4 22:00'
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

drive = webdriver.Chrome()
drive.get('https://xdclass.net')
sleep(2)
# 定位鼠标移动到上面的元素
menu_list = drive.find_element_by_css_selector('#banner_left_ul > a:nth-child(1) > li')
ActionChains(drive).move_to_element(menu_list).perform()

# 对定位到的元素选中吧子菜单
sub_menu_list = drive.find_element_by_css_selector('#active > div.des_top > div.des_one > div.des_text > a:nth-child(1)')
sleep(3)
sub_menu_list.click()

 

猜你喜欢

转载自www.cnblogs.com/zhoulixiansen/p/9906499.html