最近在用appium做自动化时发现,有一些元素无法通过uiautomatorviewer进行定位,这样就只能通过相对坐标来进行定位了。但是,问题又来了:如何获取元素的坐标呢?

在网上找了半天也没找到相应的解决方法,后来在一篇文章中看到打开手机指针位置来确定元素所在坐标。具体方法:设置--开发者选项--指针位置

开启指针位置之后,点击手机屏幕就会显示该位置的具体坐标,这样就获取到了元素的绝对坐标 然后通过webdriver的tap()函数点击该坐标就可以了。

我们获取到的是绝对坐标,如果换一个屏幕分辨率不同的手机那这个坐标自然会发生变化,要实现不同手机均能实现点击同一控件自然要用到相对坐标了,具体方法如下:

1.获取当前空间的绝对坐标(x1,y1),开启指针位置后,通过点击控件位置获取坐标;

2.获取当前手机的屏幕大小(x2,y2),通过driver.get_window_size()['width'],dirver.get_window_size()['height']分辨获取当前手机的x、y坐标;

3.获取测试手机的屏幕大小(x3,y3),获取方式同上一步;

4.获取指定控件在测试手机中的坐标:((x1/x2)*x3,(y1/y2)*y3)

5.获取到坐标之后同样使用tap()函数点击该控件。

具体代码如下:

# -*-encoding:utf-8 -*-

import os
import unittest
import HTMLTestRunner
from test_platform import URL
from test_platform import platform
from appium import webdriver
from time import sleep

# 设定系数
a = 554.0/1080
b = 1625.0/1794

class Login(unittest.TestCase):
def setUp(self):
platform['appPackage'] = 'com.xxxxxxxxx'
platform['appActivity'] = '.ui.activity.LoginActivity'
#platform['appWaitActivity'] = '.MainActivity_'

desired_caps = platform
self.driver = webdriver.Remote(URL,desired_caps)

self.driver.implicitly_wait(30)

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

def test_login(self):
el = self.driver.find_element_by_xpath("\
//android.widget.TextView[contains(@resource-id,'com.xxxxx/etRole')]")
if el:
el.click()
# 以xml格式打印当前页面内容
# print self.driver.page_source.encode("utf-8")
# self.driver.find_element_by_name("家长").click()

# 获取当前手机屏幕大小X,Y
X = self.driver.get_window_size()['width']
Y = self.driver.get_window_size()['height']
# 屏幕坐标乘以系数即为用户要点击位置的具体坐标,当前app内为选择用户角色为“家长”
self.driver.tap([(a*X, b*Y)],)
# 输入手机号码
self.driver.find_element_by_xpath("\
//android.widget.EditText[contains(@resource-id,'com.xxxxx:id/etAccount')]")\
.send_keys("***********")

# 输入密码
self.driver.find_element_by_xpath("\
//android.widget.EditText[contains(@resource-id,'com.xxxxxx.qh:id/etPasswd')]") \
.send_keys("123456")

# 点击登录按钮
self.driver.find_element_by_xpath("\
//android.widget.Button[contains(@resource-id,'com.xxxxxx:id/btnLogin')]").click()

sleep(5)

# 当前账号存在多个孩子,选择孩子后登录app
self.driver.find_element_by_xpath("\
//android.widget.TextView[contains(@text,'徐熙媛')]").click()

if __name__ == '__main__':
unittest.main()

本文转自:https://blog.csdn.net/qq_37695583/article/details/79320116

appium使用相对坐标定位元素的更多相关文章

  1. Python+Appium自动化测试(12)-通过坐标定位元素

    在使用appium做app自动化测试的过程中,可能会遇到元素的属性值不是唯一的情况,导致不能通过find_element_bi_xx()方法定位元素,这个时候我们就可以通过坐标来定位元素. 1,通过绝 ...

  2. Appium学习路—Android定位元素与操作

    一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...

  3. 【appium】根据xpath定位元素

    1. 背景 本文尝试使用的试验对象是SDK自带的NotePad应用实例,假设已经有两个Notes分别是“note1”和“note2”添加到Notepad上面,我们要做的就是尝试用xpath的方法来定位 ...

  4. appium+python测试app使用相对坐标定位元素

    我们获取到的是绝对坐标,如果换一个屏幕分辨率不同的手机那这个坐标自然会发生变化,要实现不同手机均能实现点击同一控件自然要用到相对坐标了,具体方法如下: 1.获取当前空间的绝对坐标(x1,y1),开启指 ...

  5. 【appium】根据UIAutomator定位元素

    text属性的方法 driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View") ...

  6. 【appium】根据accessibility_id定位元素

    如何获得AccessibilityId 可以通过UIAutomatorViewer或者Appium Inspector获得.Accessibility ID在Android上面就等同于contentD ...

  7. appium使用H5怎么定位元素

    允许是一个class,如果遇到有多个class,只能填写一个. 对于移动端H5元素定位采用Chromedriver的解决方案,具体操作如下: 1.手机安装Chrome浏览器 2.开启USB调试模式,并 ...

  8. 【appium】根据UIAutomator定位元素等等资料

    https://www.cnblogs.com/paulwinflo/p/4742529.html http://www.cnblogs.com/meitian/p/6103391.html http ...

  9. Appium+Python自动化 2 定位元素方式

    1.找到 Android SDK安装路径tools 下面的 uiautomatorviewer.bat,如下截图 2.点击uiautomatorviewer.bat进行启动,左上角一共四个按钮,作用分 ...

随机推荐

  1. apache 伪静态配置 .htaccess

    htaccess语法教程apache服务器伪静态规则教程 虽然网上有很多教程,不过发现大部分都是抄袭一个人的,一点都不全,所以我想写一个简单的易于理解的教程,我学习.htaccess是从目录保护开始的 ...

  2. 文献管理器endnote学习笔记

    目录 文献管理器endnote学习笔记 一.文献信息输入(将文献信息添加到文献管理软件endnote的多种方法) 1.在线检索(方便快捷,但有些网站无法直接检索) 2.网站输出(所有网站都支持的方式, ...

  3. windows下快速安装tensorflow

    下载安装文件 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 配置pip参数文件 [global] index-url = https://mi ...

  4. 7)get方式提交表单和简单处理

    一个带有html的代码:   hello.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  5. Seconds from winning

    英语沙龙 1. Who winned the game? Answer: It was not Paxton who would hoist the winner's trophy. Paxton h ...

  6. flash插件的安装——网页视频无法播放

    1.从官网下载Adobe flash player 安装包.官方网址:https://get.adobe.com/cn/flashplayer/ 或者从我的网盘下载:链接:https://pan.ba ...

  7. 远程关机 (Windows shutdown Windows)

    在某些场景,可使用远程关机控制整个局域网中的所有电脑进行一键关机或重启,便于管理,以提高工作效率. 从远程系统强制关机,首先需要进行一些必要的设置. 1.使用 win + R 打开运行,输入gpedi ...

  8. Pay Back(模拟)

    链接:https://ac.nowcoder.com/acm/contest/1086/C 题目描述 "Never a borrower nor a lender be." O h ...

  9. Ubuntu16.04中Mysql 5.7 安装配置

    记录在Ubuntu 16.04安装Mysql 5.7时遇到的一些问题. Mysql安装 使用如下命令进行安装: 1 sudo apt-get install mysql-server mysql-cl ...

  10. GSON转换成Long型变为科学计数法及时间格式转换异常的解决方案

    直接上工具类了,简单实用 public class GsonUtils { private static Gson gson = null; static { if (gson == null) { ...