Selenium 鼠标/键盘操作

鼠标操作

需要导入ActionChais类

from selenium.webdriver.common.action_chains import ActionChains

在这里插入图片描述
ActionChains具体的方法如上,下面介绍几种常用的:

#基本语法如下
action=ActionChains(driver)#初始化
action.方法(WebElement对象).perform()#一定要加perform释放,事件才会生效

双击double_click

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试双击')
ActionChains(dr).double_click(dr.find_element('id','su')).perform()

鼠标悬停move_to_element

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from  time import sleep
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
wait=WebDriverWait(dr,50)
ActionChains(dr).move_to_element(dr.find_element('id','s-usersetting-top')).perform()#鼠标悬停
sleep(2)
wait.until(expected_conditions.element_to_be_clickable(("xpath","//*[text()='高级搜索']"))).click()

拖拽drag_and_drop,需要两个参数,拖拽的起点与终点

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from  time import sleep
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
dr.find_element('id','su').click()
sleep(2)
source=dr.find_element_by_class_name('nums_text')
target=dr.find_element('xpath',"//*[text()='帮助']")
ActionChains(dr).drag_and_drop(source,target).perform()

右击context_click

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
ActionChains(dr).context_click(dr.find_element('id','su')).perform()

其他的就不一一介绍了,大同小异。

键盘操作

导入Keys模块
from selenium.webdriver.common.keys import Keys

看下源码

class Keys(object):
 
    NULL = '\ue000'
    CANCEL = '\ue001'  # ^break
    HELP = '\ue002'
    BACKSPACE = '\ue003'
    BACK_SPACE = BACKSPACE
    TAB = '\ue004'
    CLEAR = '\ue005'
    RETURN = '\ue006'
    ENTER = '\ue007'
    SHIFT = '\ue008'
    LEFT_SHIFT = SHIFT
    CONTROL = '\ue009'
    LEFT_CONTROL = CONTROL
    ALT = '\ue00a'
    LEFT_ALT = ALT
    PAUSE = '\ue00b'
    ESCAPE = '\ue00c'
    SPACE = '\ue00d'
    PAGE_UP = '\ue00e'
    PAGE_DOWN = '\ue00f'
    END = '\ue010'
    HOME = '\ue011'
    LEFT = '\ue012'
    ARROW_LEFT = LEFT
    UP = '\ue013'
    ARROW_UP = UP
    RIGHT = '\ue014'
    ARROW_RIGHT = RIGHT
    DOWN = '\ue015'
    ARROW_DOWN = DOWN
    INSERT = '\ue016'
    DELETE = '\ue017'
    SEMICOLON = '\ue018'
    EQUALS = '\ue019'

    NUMPAD0 = '\ue01a'  # number pad keys
    NUMPAD1 = '\ue01b'
    NUMPAD2 = '\ue01c'
    NUMPAD3 = '\ue01d'
    NUMPAD4 = '\ue01e'
    NUMPAD5 = '\ue01f'
    NUMPAD6 = '\ue020'
    NUMPAD7 = '\ue021'
    NUMPAD8 = '\ue022'
    NUMPAD9 = '\ue023'
    MULTIPLY = '\ue024'
    ADD = '\ue025'
    SEPARATOR = '\ue026'
    SUBTRACT = '\ue027'
    DECIMAL = '\ue028'
    DIVIDE = '\ue029'

    F1 = '\ue031'  # function  keys
    F2 = '\ue032'
    F3 = '\ue033'
    F4 = '\ue034'
    F5 = '\ue035'
    F6 = '\ue036'
    F7 = '\ue037'
    F8 = '\ue038'
    F9 = '\ue039'
    F10 = '\ue03a'
    F11 = '\ue03b'
    F12 = '\ue03c'

    META = '\ue03d'
    COMMAND = '\ue03d'

使用起来非常方便,需结合send_keys()一起使用
介绍下常用的按键及组合按键

回车键: Keys.ENTER

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
dr.find_element('id','su').send_keys(Keys.ENTER)

删除键: Keys.BACK_SPACE

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
dr.find_element('id','kw').send_keys(Keys.BACK_SPACE)

空格键: Keys.SPACE

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
dr.find_element('id','kw').send_keys(Keys.SPACE)

组合按键
比如常用的全选、复制、粘贴,通过CONTROL(键盘上的ctrl键)

send_keys(Keys.CONTROL,'a')   #全选(Ctrl+A)
send_keys(Keys.CONTROL,'c')   #复制(Ctrl+C)
send_keys(Keys.CONTROL,'x')   #剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v')   #粘贴(Ctrl+V)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
dr=webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()
dr.find_element('id','kw').send_keys('测试')
time.sleep(2)
dr.find_element('id','kw').send_keys(Keys.CONTROL,'a')#全选
time.sleep(2)
dr.find_element('id','kw').send_keys(Keys.CONTROL,'x')#剪切
time.sleep(2)
dr.find_element('id','kw').send_keys(Keys.CONTROL,'v')#粘贴

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

久醉绕心弦,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值