appium ios 相对坐标点击和控件滑动操作

环境:

系统:ios 10.13.6 (17G12034)

appium:1.14.0

xcode:10.1

iphone:iphone7 12.4

 

在尝试使用driver.tap([(a,b)],500)做点击操作报错(复制了一个别人的,自己的当时没有存,大致格式就是这样):

WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Unhandled endpoint: /session/9C347804-6FAD-44F7-8C25-0CE995D01EF0/wda/touch/perform -- http://localhost:8100/ with parameters {
    wildcards =     (
        "session/9C347804-6FAD-44F7-8C25-0CE995D01EF0/wda/touch/perform"
    );
}

搜索了一下是driver.tap()已经不兼容了

这边分享一下我这边测试有效的点击和滑动方法

1、相对坐标点击

def target_click(x1, y1):
    '''
    :param self:
    :param x1: x1,y1为编写脚本时适用设备的实际坐标
    :param y1:
    :return:
    '''
    a1 = x1 / 374
    b1 = y1 / 667
    # 获取当前手机屏幕大小X,Y
    X = driver.get_window_size()['width']
    Y = driver.get_window_size()['height']
    # 屏幕坐标乘以系数即为用户要点击位置的具体坐标
    driver.execute_script("mobile: tap", {"x": a1 * X, "y": b1 * Y})

调用:

target_click(117,515)

2.1、滑动-控件左划

def geteleByPredicate(con):
    '''
    通过ByPredicate方式获得元素
    :param con: condition
    :return: 元素
    '''
    ele = driver.find_element_by_ios_predicate(con)
    return ele

def toLeftSwipeByPredicate(con):
    ele = geteleByPredicate(con)
    driver.execute_script("mobile:swipe", {"direction": "left", 'element':ele , "duration": 1})

调用:

toLeftSwipeByPredicate("type == 'XCUIElementTypeStaticText' AND value CONTAINS 'xxxx'")

2.2、滑动-整个屏幕缓慢向下滑动2次(这个操作很迷)

driver.execute_script('mobile: scroll', {'direction': 'down'})

 

参考博客:https://blog.csdn.net/qq_35741999/article/details/88533812

  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Appium中,你可以使用`tap`方法来模拟对相对坐标点击操作相对坐标相对于元素的位置进行计算的,而不是整个屏幕。 以下是一个示例代码片段,展示了如何使用Appium在给定元素的相对坐标上进行点击: ```python from appium import webdriver from appium.webdriver.common.touch_action import TouchAction # 创建驱动对象 desired_caps = { 'platformName': 'Android', 'deviceName': 'your_device_name', 'appPackage': 'your_app_package', 'appActivity': 'your_app_activity' } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 定位元素 element = driver.find_element_by_id('your_element_id') # 获取元素尺寸 element_size = element.size element_width = element_size['width'] element_height = element_size['height'] # 获取元素位置 element_location = element.location element_x = element_location['x'] element_y = element_location['y'] # 计算相对坐标 relative_x = int(element_width * 0.5) # 相对于元素宽度的中心点 relative_y = int(element_height * 0.5) # 相对于元素高度的中心点 # 创建TouchAction对象并进行点击操作 action = TouchAction(driver) action.tap(x=element_x + relative_x, y=element_y + relative_y).perform() # 关闭驱动对象 driver.quit() ``` 请注意,上述代码中的`your_device_name`、`your_app_package`、`your_app_activity`和`your_element_id`需要根据你的设备、应用程序和元素进行替换。 使用上述代码,你可以在给定元素的相对坐标上进行点击操作。你可以根据需要修改相对坐标的计算方式,例如使用元素尺寸的四分之一点作为相对坐标等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值