常见的操作事件:右键点击,页面滑动,表单操作
ActionChains :执行PC端的鼠标点击,双击,右键,拖拽等动作
TouchActions:模拟PC端和移动端的点击,滑动,拖拽,多点触控等多种手势的操作
动作链接ActionChains
执行原理:调用ActionChains的方法时,不会立即执行,而是将所有的操作,按照顺序存放在一个队列里,当调用perform()方法时,队列中的时间才会一次执行
基本用法
action = ActionChains(driver)生成一个动作
action.方法1 动作添加方法1
action.方法2 动作添加方法2
action.perform 调用perform()执行
ActionChains的基本用法:
actions = ActionChains(self.driver)
actions.click(element_click)
actions.context_click(element_rightly01)
actions.double_click(element_doubleclick)
# time.sleep(3)
ActionChains的具体用法:链式用法,分布写法
链式用法:
ActionChains(self.driver).move_to_element(element).click(element).perform()
分布写法:
actions = ActionChains(self.driver)
actions.move_to_element(element)
actions.click(element)
actions.perfrom()
import time
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class TestActionChains:
def setup(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.driver.maximize_window()
print('setup')
def teardown(self):
self.driver.quit()
print('teardown')
-------------------点击,右键,双击-------------
def test_case_clicks(self):
self.driver.get('http://sahitest.com/demo/clicks.htm')
找到操作的元素
element_click = self.driver.find_element_by_xpath('//input[@value="click me"]')
element_doubleclick = self.driver.find_element_by_xpath('//input[@value="dbl click me"]')
element_rightly01 = self.driver.find_element_by_xpath('//input[@value="right click me"]')
actions = ActionChains(self.driver) 生成一个动作
actions.click(element_click) 动作添加方法:点击
actions.context_click(element_rightly01) 动作添加方法:右击
actions.double_click(element_doubleclick) 动作添加方法:双击
# time.sleep(3)
actions.perform() 执行actions
time.sleep(3)
-------------------鼠标移动到----------------
def test_case_moveto(self):
self.driver.get('https://www.baidu.com/')
element_moveto = self.driver.find_element(By.ID, 's-usersetting-top')
actions = ActionChains(self.driver)
actions.move_to_element(element_moveto)
actions.perform()
sleep(3)
-------------------点击不放,拉到某个位置,放开------
def test_drag_drop(self):
self.driver.get('http://sahitest.com/demo/dragDropMooTools.htm')
ele_drag = self.driver.find_element(By.ID, 'dragger')
ele_drop = self.driver.find_element_by_xpath('/html/body/div[2]')
actions = ActionChains(self.driver)
# actions.drag_and_drop(ele_drag, ele_drop)
'''
actions.click_and_hold(ele_drag)
actions.release(ele_drop)
'''
actions.click_and_hold(ele_drag).move_to_element(ele_drop).release()
actions.perform()
sleep(3)
-------------------模拟按键方法---------------
def test_keys(self):
self.driver.get('http://sahitest.com/demo/label.htm')
ele_input = self.driver.find_element_by_xpath('/html/body/label[1]/input')
ele_input.click()
actions = ActionChains(self.driver).pause(1)
actions.send_keys('usename1').pause(1) #pause 相当于等待1秒
actions.send_keys(Keys.SPACE).pause(1)
actions.send_keys('tom').pause(1)
actions.send_keys(Keys.BACKSPACE).pause(1)
actions.perform()
TouchActions
TouchActions类似于ActionChains,TouchActions类似于ActionChains。
ActionChains只是针对PC端鼠标模拟的操作,不支持H5页面
TouchChains支持H5页面操作
tap --- 在指定元素上敲击
double_tap --- 在指定元素上双敲击
tap_and_hold --- 在指定元素上点击但不释放
move --- 手势移动指定偏移(未释放)
release --- 释放手势
scroll --- 手势点击并滚动
scroll_from_element --- 从某个元素位置开始手势点击并滚动
long_press --- 长按元素
flick --- 手势滑动
flick_element --- 从某个元素位置开始手势滑动
-----------------------------------
from time import sleep
from selenium import webdriver
from selenium.webdriver import TouchActions
from selenium.webdriver.common.by import By
class TestTouchActions:
def setup(self):
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
self.driver = webdriver.Chrome(options=option)
self.driver.implicitly_wait(5)
self.driver.maximize_window()
def teardown(self):
pass
#self.driver.quit()
def test_touchaction_scrollbotttom(self):
self.driver.get('https://www.baidu.com/')
ele = self.driver.find_element(By.ID, 'kw')
elem = self.driver.find_element(By.ID, 'su')
ele.send_keys('selenium测试')
action = TouchActions(self.driver)
action.tap(elem)
action.perform()
action.scroll_from_element(ele, 0, 10000).perform()
sleep(3)