Appium+Python3 元素定位

id/name/classname见元素定位以下案例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-21 16:31
# @File : kyb_login.py
'''
通过调用capability.py文件登录考研帮app
'''
from find_element.capability import driver,NoSuchElementException

def login():
    driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear()
    # driver.find_element_by_name('请输入用户名').clear()
    # driver.find_element_by_class_name('android.widget.EditText').click()
    driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('自学网2018')

    driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('zxw2018')
    driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click()

try:
    myself=driver.find_element_by_id('com.tal.kaoyan:id/mainactivity_button_mysefl')
except NoSuchElementException:
    login()
else:
    myself.click()
    driver.find_element_by_id('com.tal.kaoyan:id/activity_usercenter_username').click()
    login()

用Appium-desktop查看页面元素或者用SDK自带的uiautomatorviewer.bat定位页面元素

list定位如下案例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-20 11:35
# @Author : zhouyang
# @File : kyb_test.py
'''
功能:在模拟器上安装app然后打开
'''
'''
复制粘贴当前行Ctrl+D
注释Ctrl+/
运行当前脚本Ctrl+Shift+f10
方法定义跳转Ctrl+B
'''
from appium import webdriver 
from selenium.common.exceptions import NoSuchElementException

desired_caps={}
desired_caps['platformName']='Android'
# desired_caps['platformVersion']='4.4.2'
desired_caps['platformVersion']='5.1.1'
# desired_caps['deviceName']='127.0.0.1:62001'
desired_caps['deviceName']='127.0.0.1:62025'

desired_caps['app']=r'D:360安全浏览器下载apkkaoyan3.1.0.apk'
desired_caps['appPackage']='com.tal.kaoyan'
desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity'

# desired_caps['noReset']='True'
desired_caps['noReset']='False'#相当于首次登录
#账号涉及中文,所以需要以下设置
desired_caps['unicodeKeyboard']='True'
desired_caps['resetKeyboard']='True'

desired_caps['automationName']='uiautomator2'

driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(3)


def check_cancelBtn():
    print('start check cancelBtn')
    try:
        cancelBtn = driver.find_element_by_id('android:id/button2')
    except NoSuchElementException:
        print('no cancelBtn')
    else:
        cancelBtn.click()

def check_skipBtn():
    print('start check skipBtn')
    try:
        skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip')
    except NoSuchElementException:
        print('no skipBtn')
    else:
        skipBtn.click()

check_cancelBtn()
check_skipBtn()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-24 15:17
# @Author : zhouyang
# @File : by_relative.py
'''
相对定位及list定位
注:该案例用于desired_caps['noReset']='False'首次登录
功能:选择头像并注册用户
'''
from find_element.capability import driver
import random
#设置头像
driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click()#点击注册
root_element=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_parentlayout')
root_element.find_element_by_class_name('android.widget.ImageView').click()#点击头像按钮
#获取所有图片,选择第二个
images=driver.find_elements_by_id('com.tal.kaoyan:id/item_image')
images[2].click()#索引从0开始
#点击保存
driver.find_element_by_id('com.tal.kaoyan:id/save').click()

#填写注册信息
#用户名
username='zxw2018'+str(random.randint(100,999))
print('uername:%s' %username)
driver.find_element_by_id('com.tal.kaoyan:id/activity_register_username_edittext').send_keys(username)
#密码
password='zxw2018'+str(random.randint(100,999))
print('password:%s' %password)
driver.find_element_by_id('com.tal.kaoyan:id/activity_register_password_edittext').send_keys(password)
#email
email='51zxw'+str(random.randint(1000,9999))+'@162.com'
print('email:%s' %email)
driver.find_element_by_id('com.tal.kaoyan:id/activity_register_email_edittext').send_keys(email)
#点击注册
driver.find_element_by_id('com.tal.kaoyan:id/activity_register_register_btn').click()

#院校选择
#年份
driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_time').click()
driver.find_elements_by_xpath('//*[@class="android.widget.TextView"]')[5].click()
#院校
driver.find_element_by_id('com.tal.kaoyan:id/perfectinfomation_edit_school_name').click()
driver.find_elements_by_id('com.tal.kaoyan:id/more_forum_title')[1].click()
driver.find_elements_by_class_name('android.widget.LinearLayout')[1].click()
#专业
driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_major').click()
driver.find_elements_by_id('com.tal.kaoyan:id/major_subject_title')[1].click()
driver.find_elements_by_id('com.tal.kaoyan:id/major_group_title')[2].click()
driver.find_elements_by_id('com.tal.kaoyan:id/major_search_item_name')[1].click()
#进入考研帮
driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_goBtn').click()

xpath定位如下案例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-24 15:37
# @Author : zhouyang
# @File : by_xpath.py
'''
登录功能
通过xpath定位
1.xpath路径表达式
/    从根节点选取
//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
nodename   选取此节点的所有子节点
.   选取当前节点
..  选取当前节点的父节点
@   选取属性

2.xpath匹配符
*   匹任何元素节点
@*  匹配任何属性节点
node()  匹配任何类型的节点
'''
from find_element.capability import driver
driver.find_element_by_xpath('//android.widget.EditText[@text="请输入用户名"]').send_keys('zxw1234')
#表示在android.widget.EditText节点下匹配响应的text属性
driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('zxw123456')
#*表示在所有节点下匹配class和index属性
driver.find_element_by_xpath('//android.widget.Button').click()
# driver.find_element_by_xpath('//*[@class="android.widget.Button"]').click()
#功能相同

UiAutomator元素定位

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-25 16:02
# @Author : zhouyang
# @File : by_Uiautomator.py
'''
用Uiautomator定位元素登陆
'''
from find_element.capability import driver
# 用id定位
# driver.find_element_by_android_uiautomator
#     ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys('zwx1234')
# 用text定位
# driver.find_element_by_android_uiautomator
#      ('new UiSelector().text("请输入用户名")').send_keys('zwx1234')
#用classname定位
driver.find_element_by_android_uiautomator
     ('new UiSelector().className("android.widget.EditText")').send_keys('zwx1234')

driver.find_element_by_android_uiautomator
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('zxw123456')

driver.find_element_by_android_uiautomator
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()

H5元素定位

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-06-26 16:24
# @Author : zhouyang
# @File : by_H5.py
'''
定位H5页面上的元素,并且在H5页面和原生元素见切换
注;需要在app上安装chrome,定位H5页面时需用selenium定位方法定位,模拟器的话需要5.0以上,逍遥模拟器
'''
# from appium import webdriver
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

desired_caps={}
desired_caps['platformName']='Android'
desired_caps['platformVersion']='5.1.1'
desired_caps['deviceName']='127.0.0.1:21503'
#连接逍遥模拟器
desired_caps['app']=r'D:360安全浏览器下载apkdr.fone3.2.0.apk'
desired_caps['appPackage']='com.wondershare.drfone'
desired_caps['appActivity']='com.wondershare.drfone.ui.activity.WelcomeActivity'

driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
driver.implicitly_wait(5)
#点击backup
print('click Backup')
driver.find_element_by_id('com.wondershare.drfone:id/btnBackup').click()

#等待next菜单,点击
WebDriverWait(driver,10).until(lambda x:x.find_element_by_id('com.wondershare.drfone:id/btnRecoverData'))
print('click nextBtn')
driver.find_element_by_id('com.wondershare.drfone:id/btnRecoverData').click()

#等待H5(webview)页面
WebDriverWait(driver,8).until(lambda x:x.find_element_by_class_name('android.webkit.WebView'))
contexts=driver.contexts
print(contexts)

#进入H5页面进行操作
print('switch context')
driver.switch_to.context('WEBVIEW_com.wondershare.drfone')
print('edit email')
driver.find_element_by_id('email').send_keys('2345dgf@163.com')
print('click sendBtn')
driver.find_element_by_class_name('btn_send').click()

#切换webview返回
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_class_name('android.widget.ImageButton').click()
原文地址:https://www.cnblogs.com/xiuxiu123456/p/11320107.html