1、鼠标事件:
每个模拟事件后需加.perform() 才会执行 context_click() 右击 double_click() 双击 drag_and_drop(source, target) 拖动 move_to_element() 鼠标悬停 move_to 移动1、鼠标事件: 每个模拟事件后需加.perform() 才会执行 context_click() 右击 double_click() 双击 drag_and_drop(source, target) 拖动 move_to_element() 鼠标移动到某元素 move_to 移动
2、键盘事件
send_keys(Keys.BACK_SPACE) 删除键(BackSpace) send_keys(Keys.SPACE) 空格键(Space) send_keys(Keys.TAB) 制表键(Tab) send_keys(Keys.ESCAPE) 回退键(Esc) send_keys(Keys.ENTER) 回车键(Enter) 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) send_keys(Keys.F1) 键盘F1 Send_keys(Keys.F5)键盘F5 send_keys(Keys.F12) 键盘F12
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def test_mouse(self):
self.driver.get('https://sahitest.com/demo/clicks.htm')
doublebtn = self.driver.find_element_by_xpath('/html/body/form/input[2]')
ActionChains(self.driver).double_click(doublebtn).perform()
sleep(3)
btn = self.driver.find_element_by_xpath('/html/body/form/input[3]')
ActionChains(self.driver).click(btn).perform()
sleep(3)
rightbtn = self.driver.find_element_by_xpath('/html/body/form/input[4]')
ActionChains(self.driver).context_click(rightbtn).perform()
sleep(3)
def test_key(self):
self.driver.get('http://www.baidu.com')
kw = self.driver.find_element_by_id('kw')
kw.send_keys('selenium')
kw.send_keys(Keys.CONTROL,'a')
sleep(2)
kw.send_keys(Keys.CONTROL,'x')
sleep(2)
kw.send_keys(Keys.CONTROL, 'v')
sleep(2)
bdsy = self.driver.find_element_by_link_text('百度首页')
ActionChains(self.driver).move_to_element(bdsy).perform()
if __name__ == '__main__':
case = TestCase()
case.test_key()
|