Appium basics - simulate gesture click coordinates

1. Simulate gesture click coordinates

When locating elements, you have resorted to class 18 martial arts and still cannot locate them, what should you do? (Interviews often ask)
Then come up with a trick: click on the coordinates of the location of the element. (coordinate positioning)

Detailed description:

Locate the element "Go and see" as shown in the figure below. Except for the coordinate attribute, there is no other attribute information that can be used for positioning. At this time, you need to use coordinates to locate and click.

You can see bondsthe properties on the right side in the figure below: [374,831][654,906], my understanding is that the first coordinate is the coordinate of the upper left corner of the rectangle with the red frame, and the second coordinate is the coordinate of the lower right corner of the rectangle with the red frame, so if I want to click on the red frame Content, as long as any coordinate between [374654, 831906] can be used.
 

2. tap() usage

This method is not a method in the class tap()we said before , this method is a method for simulating gesture touch.TouchActiontap()tap()

tap()Method introduction:

# 可模拟手指点击(最多五个手指),可设置按住时间长度(毫秒) :
tap(self, positions, duration=None)

参数Args: 
    positions : list(列表)类型,里面对象是元组,最多五个。
        如:[(100, 20), (100, 60)],
        元组中一个元素表示一个坐标, 元组中最多可有5个坐标。   
    duration : 持续时间,单位毫秒,如:500 
    
# 调用方式
driver.tap([(100, 20), (100, 60), (100, 100)], 500)

3. Practice

"""
1.学习目标
    熟悉tap方法使用,当元素没有任何明确属性的时候,可以使用,来定位元素,
2.操作步骤
    2.1 熟悉tap方法:触摸,不同于TouchAction中的tap方法
    2.2 调用方式
        driver.tap(positions,duration=None)
    2.3 说明:
        driver.tap([坐标,最多5个坐标],持续时间)
        参数:
            --positions 坐标 列表格式[(x,y),(x,y),(x,y)]
                列表中每个元组表示一个坐标
            --duration 持续时间

3.需求
    设置app中使用tap方法点击"通知"选项
"""

# 1.导入appium和TouchAction
import time
from appium import webdriver

# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
    "platformName": "Android",  # 系统名称
    "platformVersion": "7.1.2",  # 系统版本
    "deviceName": "127.0.0.1:21503",  # 设备名称
    "appPackage": "com.android.settings",  # APP包名
    "appActivity": ".Settings"  # APP启动名
}

# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 4.操作APP
# 使用tap方法点击设置app页面中"通知"选项,
# 通过元素定位工具UI Automator Viewer,可以知道"通知"的坐标范围[108,894][720,965]
# x轴 范围  108--720
# y轴 范围  894--965
# 页面等待
driver.wait_activity(".Settings", 10)

# 点击"通知"选项
driver.tap([(120, 900)], 3000)

# 注:持续时间有和没有,在效果上区别不是很大。

# 5.关闭APP
time.sleep(3)
driver.quit()

hint:

Because the method that has been used before click()is to click, click()the method is to click after the element is positioned.

The coordinate information in the page always exists, so tap()the method will run directly, so when the script is executed, there is no click effect, because the element has not yet appeared when the coordinate positioning click operation is performed.

Therefore, tap()a delay or judgment must be added in front of the touch method! ! !

4. Disadvantages

Positioning by coordinates is the worst way to locate elements. There is really no other way to use this. In addition, if you change to another mobile phone, the resolution will be different. The coordinates cannot be written to death, and the ratio of the screen must be calculated.

tap()Note: Do not use the touch method unless it is absolutely necessary , because the resolution of different mobile phones is different, and the position of the same element is different on different mobile phones.

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/128487684