Appium 新手请教:appium+python 如何实现用坐标在安卓手机屏幕上的点击事件

莫小贝 · 2014年11月04日 · 最后由 大王命我来巡山 回复于 2016年07月12日 · 4319 次阅读

公司新开发的一个安卓应用,必须实现关于状态栏上的点击事件,用 uiautomatorviewer 找不到任何控件的信息,什么 resource id、tag name、class name、name 根本没有,只能用坐标点击,appium + python 怎么实现用坐标在安卓手机屏幕上的点击事件?
求教各位大神.......
如下图,实现绿色圆圈处的点击事件,UIautomatorviewer 里没有任何信息.....

共收到 17 条回复 时间 点赞

用 hierarchyviewer 可以看坐标,点击用 driver.tap 应该可以,你可以试试

没看懂,绿圈那里有直接点击事件?
展开后的通知栏才能用 UIautomatorviewer 看。
driver.openNotifications();可以展开通知栏。

#3 楼 @sanlengjingvv 对,绿圈那里有点击事件。
已经按照@strayeagle的方法获取了坐标,且用 self.driver.swipe(65,10,65,10,5) 可以实现点击事件了,感谢各位的帮忙
但是我要完成下面四次的点击,为啥第一个点击事件完成后就报错 WebDriverException: Message: u'An unknown server-side error occurred while processing the command.' 错误提示行就是指 self.driver.swipe(65,10,65,10,5)

self.driver.swipe(65,10,65,10,5)
sleep(5)
self.driver.swipe(254,18,254,18,5)
sleep(5)
self.driver.swipe(470,13,470,13,5)
sleep(5)
self.driver.swipe(648,16,648,16,5)
sleep(5)

public static void clickScreen(int x, int y, int duration,
AppiumDriver drivers) {
JavascriptExecutor js = (JavascriptExecutor) drivers;
HashMap tapObject = new HashMap();
tapObject.put("x", x);
tapObject.put("y", y);
tapObject.put("duration", duration);
js.executeScript("mobile: tap", tapObject);
} 自己改吧你

#5 楼 @appium_bob swipe() 方法不好用吗

你的意思是让我按照你上面的方法,改成 python 写法吗

#6 楼 @sophia_sun1191 他的意思是给了你 java 语言实现点击操作的参考,用的是 tap 方法,而不是要你去使用 python 语言去实现这个功能。
贴出代码与更详细的日志,别人才能帮你的吧。贴吧,骚年

#6 楼 @sophia_sun1191 看清楚 这个 是点击屏幕的方法 不是 swipe 方法·

#8 楼 @appium_bob swipe 把最后的时间设置短一些也可以完成点击屏幕的操作,用 tap 方法一样报 unknown server side error
driver.tap([(100, 20), (100, 60), (100, 100)], 500), 话说这个 tap 方法里面的(100,100)是啥

#10 楼 @xiaomayi0323 ,我也知道是坐标,我就点击一个地方的坐标(65,10),为啥要写三个坐标??
github 上找到 tap 的用法如下:
https://github.com/appium/python-client
tap

The tap method stands alone, being unable to be chained with other methods. If you need a tap-like action that starts a longer chain, use press.

It can take either an element with an optional x-y offset, or absolute x-y coordinates for the tap, and an optional count.

el = self.driver.find_element_by_accessibility_id('Animation')
action = TouchAction(self.driver)
action.tap(el).perform()
el = self.driver.find_element_by_accessibility_id('Bouncing Balls')
self.assertIsNotNone(el)

or absolute x-y coordinates for the tap, and an optional count. 这句不是说可以用坐标 tap 吗 为啥我用报错,代码如下:
def test_someActions(self):
sleep(8)
buttons = self.driver.find_elements_by_class_name("android.widget.Button")
buttons[1].click()
action = TouchAction(self.driver)
action.tap(65,10).perform()
sleep(5)

#11 楼 @sophia_sun1191 你报的是 python 的语法错误,int 类型没有 id 这个属性,是你代码上的问题,语法有问题了,莫抱怨 tap 的问题。

#12 楼 @strayeagle 那请问我下面的代码哪里语法错误?实在是没有看出来,谢谢! 错误提示行是 action.tap(65,10).perform(), (65,10) 是我要点击的坐标

def test_someActions(self):
sleep(8)
buttons = self.driver.find_elements_by_class_name("android.widget.Button")
buttons[1].click()
action = TouchAction(self.driver)
action.tap(65,10).perform()
sleep(5)

#13 楼 @sophia_sun1191
第一个问题:driver.tap([(100, 20), (100, 60), (100, 100)], 500), 话说这个 tap 方法里面的(100,100)是啥?
每一组坐标代表一个手指,上面的就代表有三个手指做 Tap 操作,所以 (100, 100) 也是其中的一个坐标
第二个问题:使用 tap 方法,直接 self.driver.tap([(65,10),]) 就可以了啊,其中坐标需要以元组方式传入

#13 楼 @sophia_sun1191 楼上正解,我在一楼回复的时候就贴了 tap 的 usage 方法:driver.tap([(100, 20), (100, 60), (100, 100)], 500),表示模拟 3 个坐标(也就是 3 个手指),持续时间 500 毫秒

def test_someActions(self):
    sleep(8)
    buttons = self.driver.find_elements_by_class_name("android.widget.Button")
    buttons[1].click()
    self.driver.tap([(65,10),])

#14 楼 @xiaomayi0323 @strayeagle 豁然领悟,非常感谢

#1 楼 @strayeagle 请问 appium 类似这种布局的 要怎么点击

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册