麻烦老师看到先给学生解惑,卡在这两天了
问题描述
- 环境
- Macbook
- appium 1.22.3
- 三星 Android 12
- 问题复述
- appium 开启端口 4723 报错,开启 7980 可以运行但无法识别元素
- 端口更换为 7980 后,能调起 app,但无法识别元素
相关log文件
- appium 端口为 4723 时的报错
python_error.log (32.2 KB) - appium 端口更改为 7980 报错
error.log (21.1 KB)
python_error_2.log (9.9 KB)
相关代码
import os
from time import sleep
from androguard.core.bytecodes.apk import APK
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
apk_path = '/Users/ycb/Downloads/pkg/MIYA.V4.8.0.3663-Google.apk'
def get_devices() -> list:
all_devices = []
cmd = 'adb devices'
result = os.popen(cmd).readlines()[1:]
for item in result:
if item != "\n":
all_devices.append(str(item).split('\t')[0])
return all_devices
def get_platform(dev: str) -> str:
cmd = f'adb -s {dev} shell getprop ro.build.version.release'
result = os.popen(cmd).readlines()[0]
return str(result).split('\n')[0]
def get_apkname(apk):
a = APK(apk, False, 'r')
return a.get_package()
def get_apk_lautc(apk):
a = APK(apk, False, 'r')
return a.get_main_activity()
def isinstallapk(packname: str, devname: str) -> bool:
cmd = f'adb -s {devname} shell pm list package -3'
result = os.popen(cmd).readlines()
all_apkname = []
for i in result:
apkname = str(i).split('\n')[0].split(':')[1]
all_apkname.append(apkname)
if packname in all_apkname:
return True
return False
def uninstallapk(packname: str, devname: str) -> bool:
cmd = f'adb -s {devname} shell pm list package -3'
result = os.popen(cmd).readlines()
all_apkname = []
for i in result:
apkname = str(i).split('\n')[0].split(':')[1]
all_apkname.append(apkname)
if packname in all_apkname:
cmd = f'adb -s {devname} uninstall {packname}'
os.system(cmd)
return True
return False
def installapk(packnamepath: str, devname: str) -> bool:
cmd = f'abd -s {devname} install {packnamepath}'
os.system(cmd)
return True
def test_miya_login():
packname = get_apkname(apk_path)
dev = get_devices()[0]
is_first_install = False
is_install = isinstallapk(packname, dev)
if is_install is False:
installapk(apk_path, dev)
is_first_install = True
apkname = get_apkname(apk_path)
launcheractivity = get_apk_lautc(apk_path)
desired_caps = {
'platformName': 'android',
'deviceName': dev,
'platformVersion': get_platform(dev),
'appPackage': apkname,
'appActivity': launcheractivity,
'chromeOptions' : {'w3c': False},
'showChromedriverLog': True
}
driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
sleep(1)
# if is_first_install:
# sleep(50)
# sleep(10)
driver.find_element()
username = driver.find_element(MobileBy.ID, 'com.airlive.miya:id/edt_account')
username.clear()
username.send_keys('k99')
login = driver.find_element(MobileBy.ID, 'com.airlive.miya:id/btnLogin')
login.click()
driver.close()