selenium Webdriver自动化测试之手势操作TouchAction 详解

TouchAction,类似于ActionChains,ActionChains只是针对PC端程序鼠标模拟的一系列操作,对H5页面操作是无效的。TouchAction可以对H5页面操作,通过TouchAction可以实现点击、滑动、拖拽、多点触控,以及模拟手势等各种操作。

关于 ActionChains的介绍可移步:  https://www.cnblogs.com/feng0815/p/8344120.html

手势控制

1、按压控件

方法:

  • press()

开始按压一个元素或坐标点(x,y)。通过手指按压手机屏幕的某个位置。

press(WebElement el, int x, int y)

press也可以接收屏幕的坐标(x,y)。

例:TouchAction(driver).press(x=0,y=308).release().perform()

除了press()方法之外,本例中还用到了别外两个新方法。

  • release() 结束的行动取消屏幕上的指针。

  • Perform() 执行的操作发送到服务器的命令操作。

2、长按控件

方法:

  • longPress()

开始按压一个元素或坐标点(x,y)。 相比press()方法,longPress()多了一个入参,既然长按,得有按的时间吧。duration以毫秒为单位。1000表示按一秒钟。其用法与press()方法相同。

longPress(WebElement el, int x, int y, Duration duration)

例: 

  1. TouchAction action = new TouchAction(driver);
  2. action.longPress(names.get(200),1000).perform().release();
  3. action.longPress(200 ,200,1000).perform().release();

3、点击控件

方法:

  • tap()

对一个元素或控件执行点击操作。用法参考press()。

tap(WebElement el, int x, int y)

例:

  1.  TouchAction action = new TouchAction(driver);
  2. action.tap(names.get(200)).perform().release();
  3.  action.tap(200 ,200).perform().release();

4、移动

方法:

  • moveTo()

将指针(光标)从过去指向指定的元素或点。

movTo(WebElement el, int x, int y)

其用法参考press()方法。

例:

  1.  TouchAction action = new TouchAction(driver);
  2.  action.moveTo(names.get(200)).perform().release();
  3.  action.moveTo(200 ,200).perform().release();

5、暂停

方法:

  • wait()

暂停脚本的执行,单位为毫秒。

action.wait(1000);
复制代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_TouchAction.py
@time:2020/10/17
"""

import time
from selenium import webdriver
from selenium.webdriver import TouchActions

class TestTouchAction():

def setup_method(self):
option
= webdriver.ChromeOptions()
option.add_experimental_option(
'w3c', False)
self.driver
= webdriver.Chrome(options=option)
self.driver.maximize_window()
self.driver.implicitly_wait(
5)

def teardown_method(self):
self.driver.quit()

def test_touchaction_scrollbottom(self):
self.driver.get(
"https://www.baidu.com/")
el
= self.driver.find_element_by_id('kw')
el_search
= self.driver.find_element_by_id('su')
el.send_keys(
'selenium测试')
action
= TouchActions(self.driver)
action.tap(el_search)
# 点击
action.perform()
action.scroll_from_element(el, 0,
10000).perform()
time.sleep(
3)

复制代码

转载自https://www.cnblogs.com/feng0815/p/13831057.html

posted @ 2021-07-18 16:40  R-Bear  阅读(293)  评论(0)    收藏  举报